By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,456 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,456 IT Pros & Developers. It's quick & easy.

Need List Box to appear like TxxtBox in Report

RobH
P: 56
I have a report that has some Listboxes - due to the nature that these can do SQL Queries within themselves but they appear with boxes around them and the content does not wrap.

How Can I make a listbox appear like a textbox.?
Sep 21 '07 #1
Share this Question
Share on Google+
5 Replies


Jim Doherty
Expert 100+
P: 897
I have a report that has some Listboxes - due to the nature that these can do SQL Queries within themselves but they appear with boxes around them and the content does not wrap.

How Can I make a listbox appear like a textbox.?
Going on your brief posting you could iterate through the list provided by your listbox concatenating and assigning each data value to a string variable using ITEMDATA in a loop.

Each data value could be separated for instance with a comma and space between each value. You would then strip off the last comma and space at the end of the loop and assign the whole value of the variable to the textbox

My Assumptions
You have a single listbox called MyListBox that you requery each time in the Detail_Print event of your report and that this listbox has a single (1) column list
You have a single unbound textbox Called MyTextBox placed on the detail section which will be used to display the listbox contents.
You are not too concerned with efficiency (in effect you are handling this data twice! once to return and display in a listbox and then two to loop and display the same data in a textbox.) I assume you will hiding the listbox? given the textbox will be displaying its content.

The Code to make this work in the DETAIL_PRINT event of the report

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
  3. Me!MyListBox.Requery
  4. Dim i As Integer
  5.     Dim GetList As String, strData As String
  6.     ' Iterate through all entries.
  7.     For i = 0 To MyListBox.ListCount - 1
  8.         GetList = Me!MyListBox.ItemData(i)
  9.         ' Add a comma and space to each concatenated data value.
  10.         strData = strData & GetList & ", "
  11.     Next i
  12.     If Len(strData) = 0 Then
  13.         Me!MyTextBox = ""
  14.     Else
  15.     'strip off the last space and comma and display in text box.
  16.         Me!MyTextBox = Left(strData, Len(strData) - 2)
  17.     End If
  18. End Sub
  19.  
Hope this helps you

Regards

Jim
Sep 21 '07 #2

NeoPa
Expert Mod 15k+
P: 31,186
Rob,
You're a full member now. It would be nice if you could phrase your question a little more clearly. It's hard enough to find the answers sometimes, but we shouldn't need to spend so much time trying to understand what the question means.

MODERATOR.
Sep 21 '07 #3

RobH
P: 56
Wow I see what you mean about slowing it down..
If you have a bunch on the screen it will take a while.

Thanks.
Sep 21 '07 #4

RobH
P: 56
Alternatively is there a way (in vb) to store the result of the SQL statement into the Textbox..

eg:
Expand|Select|Wrap|Line Numbers
  1. Me.MyTextBox = DoCmd.RunSQL "Select............"
  2.  
  3.  
  4. or
  5.  
  6.  
  7. Dim sqlvalue as string
  8. sqlvaue = Docmd.RunSQL "Select............"
  9. me.MyTextBox = sqlvalue
  10.  
Sep 21 '07 #5

Jim Doherty
Expert 100+
P: 897
Alternatively is there a way (in vb) to store the result of the SQL statement into the Textbox..

eg:
Expand|Select|Wrap|Line Numbers
  1. Me.MyTextBox = DoCmd.RunSQL "Select............"
  2.  
  3.  
  4. or
  5.  
  6.  
  7. Dim sqlvalue as string
  8. sqlvaue = Docmd.RunSQL "Select............"
  9. me.MyTextBox = sqlvalue
  10.  
Rob

I'm afraid you are way off the mark with that one I understand your thought process, but you are talking about the return of recordsets and how you deal with the physical data returned as a two dimensional matrix.

Docmd.RunSQL whilst incredibly useful is not the panacea to all it runs 'Action' or data definition queries
INSERT INTO, DELETE, SELECT...INTO, UPDATE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, or DROP INDEX statements

Note.... NOT.... SELECT statements (which return datasets and from which your listboxs rely for their data)

So in short...you simply cannot throw a multi column two dimensional recordset into a string variable

When I mentioned you were handling the data twice that is correct. Your answer lies optimally in opening up a RECORDSET in Code and looping through the recordset, grabbing datavalues as you loop through the recordset (in much the same way as my earlier example to you) concatenating the results in whichever way befits your requirement ie: is it one column or two or maybe more columns that you wish to grab ..... if so how do you intend to equally space datavalues if the data is to be dumped into the textbox. IF there is more than one column and so on.

It is entirely possible for you to open a recordset loop through it and assign the result to the textbox without ever having any listbox on your report you merely have to understand the concept of recordsets generally, how they get opened in memory, how you loop through and grab their values importantly and especially within the context of what you wish to do currently how to massage and manipulate that data to FILL the textbox with it.

Unfortunately every method or property has its limitations scoped to the intention of, as in this case.......the 'control' as we appreciate that 'actually' what you are trying to do is make a textbox into a listbox.

You will probably think this is way too much work to do to achieve something which, and I would agree with you seems like a simple request.

The elegance of programming mimicry very often lies in the skilful understanding of workarounds because, as you quite rightly point out, a listbox doesnt grow and behave like a textbox by default.

Have a look at the openrecordset method in Access help

I hope this helps you a bit further

Regards

Jim
Sep 21 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.