473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Possible to change text box to list box with code?

Hi all,

What I'm trying to do is two-fold. We'll tackle one and then the other, or whichever is easiest.

First Question/Problem:

Is it possible to write VBA code that would change a text box to a (let's say) list box based on the answer to a question? For example:
Expand|Select|Wrap|Line Numbers
  1. Dim Answer As Integer
  2. Answer = MsgBox("Would you like to make multiple selections?", & _
  3.               vbCrLf & "vbYesNo + vbQuestion, "Record Selection")
  4. If Answer = vbYes Then
  5.              ' Code to change text box to list box where list box contains all
  6.              ' customer names
  7. Else
  8.              ' Set focus to the text box in question
  9. Me.CustomerName.SetFocus
  10. End If
  11.  
Second Question/Problem:

I know how to set a list box to enable mutliple selections, but what I don't know how to do is select only those names for either a report or another field. For example, let's say I have 10 customer names, but I only want a report for the 2 that I have highlighted. How would I go about doing that? Another example would be, let's say that I have 20 items of product listed in a list box and a customer buys 5 items. I've figured out how to highlight those 5 items, but not how to move those items into another field that shows what that customer has purchased.

I've heard about array variables, and I have a basic idea of what they do, but I'm still confused about how to actually implement them. Is this a situation where an array variable would solve my problem or am I still wandering around, lost in the woods?
Feb 15 '08 #1
8 4826
mshmyob
904 Expert 512MB
Answer to 1st part. Not that I know of. But you could put 2 controls in the same spot and hide them until the selection is made and then unhide the 1 you want.

2nd question - I will get back to you. I am 2 busy to look at it this minute.
Hi all,

What I'm trying to do is two-fold. We'll tackle one and then the other, or whichever is easiest.

First Question/Problem:

Is it possible to write VBA code that would change a text box to a (let's say) list box based on the answer to a question? For example:
Expand|Select|Wrap|Line Numbers
  1. Dim Answer As Integer
  2. Answer = MsgBox("Would you like to make multiple selections?", & _
  3.               vbCrLf & "vbYesNo + vbQuestion, "Record Selection")
  4. If Answer = vbYes Then
  5.              ' Code to change text box to list box where list box contains all
  6.              ' customer names
  7. Else
  8.              ' Set focus to the text box in question
  9. Me.CustomerName.SetFocus
  10. End If
  11.  
Second Question/Problem:

I know how to set a list box to enable mutliple selections, but what I don't know how to do is select only those names for either a report or another field. For example, let's say I have 10 customer names, but I only want a report for the 2 that I have highlighted. How would I go about doing that? Another example would be, let's say that I have 20 items of product listed in a list box and a customer buys 5 items. I've figured out how to highlight those 5 items, but not how to move those items into another field that shows what that customer has purchased.

I've heard about array variables, and I have a basic idea of what they do, but I'm still confused about how to actually implement them. Is this a situation where an array variable would solve my problem or am I still wandering around, lost in the woods?
Feb 15 '08 #2
missinglinq
3,532 Expert 2GB
The first part of this question is so odd that, having gotten it twice in less than 24 hours, I have to ask if it's a part of a homework assignment. At any rate, take a look at this post:

Programatically convert a textbox to combobox and back

Welcome to TheScripts!

Linq ;0)>
Feb 15 '08 #3
mshmyob
904 Expert 512MB
Sorry about the delay.

Now for the answer to your second part.

If the MultiSelect property is set to Simple or Extended, you can use the Selected property or the ItemsSelected collection to determine whether a particular item in the list is selected. The Selected property is a zero-based array that contains the selection state of each item in a list box. For example, if you wanted to determine whether the first item in a list box is selected, you would check the value of the Selected property for that item. (Since the array has already been created for you, you do not have to create the array)

The following line of code prints the value of the Selected property for the first item in a list box named List1 to the Debug window:

Expand|Select|Wrap|Line Numbers
  1. Debug.Print Me!List1.Selected(0)
  2.  
For more details on how to work with the listbox see the follwoing article from Microsoft.

Programming List Boxes


Hi all,

What I'm trying to do is two-fold. We'll tackle one and then the other, or whichever is easiest.

First Question/Problem:

Is it possible to write VBA code that would change a text box to a (let's say) list box based on the answer to a question? For example:
Expand|Select|Wrap|Line Numbers
  1. Dim Answer As Integer
  2. Answer = MsgBox("Would you like to make multiple selections?", & _
  3.               vbCrLf & "vbYesNo + vbQuestion, "Record Selection")
  4. If Answer = vbYes Then
  5.              ' Code to change text box to list box where list box contains all
  6.              ' customer names
  7. Else
  8.              ' Set focus to the text box in question
  9. Me.CustomerName.SetFocus
  10. End If
  11.  
Second Question/Problem:

I know how to set a list box to enable mutliple selections, but what I don't know how to do is select only those names for either a report or another field. For example, let's say I have 10 customer names, but I only want a report for the 2 that I have highlighted. How would I go about doing that? Another example would be, let's say that I have 20 items of product listed in a list box and a customer buys 5 items. I've figured out how to highlight those 5 items, but not how to move those items into another field that shows what that customer has purchased.

I've heard about array variables, and I have a basic idea of what they do, but I'm still confused about how to actually implement them. Is this a situation where an array variable would solve my problem or am I still wandering around, lost in the woods?
Feb 17 '08 #4
Sorry about the delay.

Now for the answer to your second part.

If the MultiSelect property is set to Simple or Extended, you can use the Selected property or the ItemsSelected collection to determine whether a particular item in the list is selected. The Selected property is a zero-based array that contains the selection state of each item in a list box. For example, if you wanted to determine whether the first item in a list box is selected, you would check the value of the Selected property for that item. (Since the array has already been created for you, you do not have to create the array)

The following line of code prints the value of the Selected property for the first item in a list box named List1 to the Debug window:

Expand|Select|Wrap|Line Numbers
  1. Debug.Print Me!List1.Selected(0)
  2.  
For more details on how to work with the listbox see the follwoing article from Microsoft.

Programming List Boxes
Thanks! That's exactly what I needed. However, I would still like to be able to see both of the columns that existed in my "selected from" list in my "selected to" list. I messed with changing the column size and count, but all that did was stack my selected items next to each other instead of showing both the UPC and the Item Description side by side as I had wanted. Here's what my code looks like that enables me to create multiple selections; maybe someone can help me figure out if it's possible to do what I'm asking. Also, I know this probably sounds stupid, but how do I preview a report based on the selections I've made? I installed a preview report button, but the report comes up blank when I click it. Hope the following code helps.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub cmdSelect1_Click()
  4.     Dim lst1 As ListBox, lst2 As ListBox
  5.     Dim itm As Variant
  6.  
  7.     Set lst1 = Me!lstProduct
  8.     Set lst2 = Me!lstSelected
  9.     ' Check selected items.
  10.     For Each itm In lst1.ItemsSelected
  11.         ' Set RowSource property for first selected item.
  12.         If lst2.RowSource = "" Then
  13.             lst2.RowSource = lst1.ItemData(itm)
  14.         Else
  15.             ' Check whether item has already been copied.
  16.             If Not InStr(lst2.RowSource, lst1.ItemData(itm)) > 0 Then
  17.                 lst2.RowSource = lst2.RowSource & ";" & lst1.ItemData(itm)
  18.             End If
  19.         End If
  20.     Next itm
  21. End Sub
  22.  
  23.  
  24. Private Sub cmdUnselect1_Click()
  25. Me!lstSelected.RowSource = ""
  26. Me!lstProduct.SetFocus
  27. Call Form_Load
  28. End Sub
  29.  
  30. Private Sub Form_Load()
  31.  
  32. DoCmd.MoveSize 2000, 1500
  33.  
  34. Dim lst1 As ListBox, lst2 As ListBox
  35. Dim strSQL As String, strMsg As String
  36.  
  37. Set lst1 = Me!lstProduct
  38. Set lst2 = Me!lstSelected
  39.  
  40. ' Initialize SQL string.
  41. strSQL = "SELECT [UPC],[ItemDescription] FROM tblProduct ORDER By UPC;"
  42.  
  43. ' Notify user if multiple selection is not enabled.
  44. If lst1.MultiSelect = 0 Then
  45.     'strMsg = "The MultiSelect property of List1 is set to None." & _
  46.     '"To enable multiple selection, open the form in Design view " _
  47.     '"and set the MultiSelect property to Simple or Extended."
  48.     MsgBox "Multiple Selection Not Enabled", vbInformation, "Multiple selection not enabled"
  49. End If
  50.  
  51. ' Fill List1.
  52. With lst1
  53.     .RowSourceType = "Table/Query"
  54.     .RowSource = strSQL
  55.     .ColumnCount = 2
  56. End With
  57. With lst2
  58. ' Specify that List2 is a value list.
  59.     .RowSourceType = "Value List"
  60.     .ColumnCount = 1
  61.     End With
  62. End Sub
  63. Private Sub cmdPreview_Click()
  64. On Error Resume Next
  65.     Dim stDocName As String
  66.     stDocName = "rptProduct"
  67.     DoCmd.OpenReport stDocName, acViewPreview
  68.     DoCmd.Maximize
  69. End Sub
By the way, I've decided I would just make one box invisible instead of trying to programmatically change the style of the box. It just seems easier to me. Thanks for the suggestion.
Feb 19 '08 #5
Thanks! That's exactly what I needed. However, I would still like to be able to see both of the columns that existed in my "selected from" list in my "selected to" list. I messed with changing the column size and count, but all that did was stack my selected items next to each other instead of showing both the UPC and the Item Description side by side as I had wanted. Here's what my code looks like that enables me to create multiple selections; maybe someone can help me figure out if it's possible to do what I'm asking. Also, I know this probably sounds stupid, but how do I preview a report based on the selections I've made? I installed a preview report button, but the report comes up blank when I click it. Hope the following code helps.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub cmdSelect1_Click()
  4.     Dim lst1 As ListBox, lst2 As ListBox
  5.     Dim itm As Variant
  6.  
  7.     Set lst1 = Me!lstProduct
  8.     Set lst2 = Me!lstSelected
  9.     ' Check selected items.
  10.     For Each itm In lst1.ItemsSelected
  11.         ' Set RowSource property for first selected item.
  12.         If lst2.RowSource = "" Then
  13.             lst2.RowSource = lst1.ItemData(itm)
  14.         Else
  15.             ' Check whether item has already been copied.
  16.             If Not InStr(lst2.RowSource, lst1.ItemData(itm)) > 0 Then
  17.                 lst2.RowSource = lst2.RowSource & ";" & lst1.ItemData(itm)
  18.             End If
  19.         End If
  20.     Next itm
  21. End Sub
  22.  
  23.  
  24. Private Sub cmdUnselect1_Click()
  25. Me!lstSelected.RowSource = ""
  26. Me!lstProduct.SetFocus
  27. Call Form_Load
  28. End Sub
  29.  
  30. Private Sub Form_Load()
  31.  
  32. DoCmd.MoveSize 2000, 1500
  33.  
  34. Dim lst1 As ListBox, lst2 As ListBox
  35. Dim strSQL As String, strMsg As String
  36.  
  37. Set lst1 = Me!lstProduct
  38. Set lst2 = Me!lstSelected
  39.  
  40. ' Initialize SQL string.
  41. strSQL = "SELECT [UPC],[ItemDescription] FROM tblProduct ORDER By UPC;"
  42.  
  43. ' Notify user if multiple selection is not enabled.
  44. If lst1.MultiSelect = 0 Then
  45.     'strMsg = "The MultiSelect property of List1 is set to None." & _
  46.     '"To enable multiple selection, open the form in Design view " _
  47.     '"and set the MultiSelect property to Simple or Extended."
  48.     MsgBox "Multiple Selection Not Enabled", vbInformation, "Multiple selection not enabled"
  49. End If
  50.  
  51. ' Fill List1.
  52. With lst1
  53.     .RowSourceType = "Table/Query"
  54.     .RowSource = strSQL
  55.     .ColumnCount = 2
  56. End With
  57. With lst2
  58. ' Specify that List2 is a value list.
  59.     .RowSourceType = "Value List"
  60.     .ColumnCount = 1
  61.     End With
  62. End Sub
  63. Private Sub cmdPreview_Click()
  64. On Error Resume Next
  65.     Dim stDocName As String
  66.     stDocName = "rptProduct"
  67.     DoCmd.OpenReport stDocName, acViewPreview
  68.     DoCmd.Maximize
  69. End Sub
By the way, I've decided I would just make one box invisible instead of trying to programmatically change the style of the box. It just seems easier to me. Thanks for the suggestion.
Subscribed to this thread.
Feb 19 '08 #6
mshmyob
904 Expert 512MB
Here is the code to have 2 list boxes. One is the 'from' list box and one is the 'to' list box. When you make the selection from list box 1 it will populate list box 2 with the same selections. In this example I had 3 columns in my 2 list boxes. All columns transfer over to the 2nd list box.

List box 2 should have the same number of columns has list box 1 but change the 'ROW SOURCE TYPE' property to VALUE LIST and make sure there is NOTHING entered into the 'ROW SOURCE' property.

The bound column in both list boxes should be the same.

Expand|Select|Wrap|Line Numbers
  1. Dim lst1 As ListBox
  2. Dim lst2 As ListBox
  3. Dim itm As Variant
  4. Dim vSearchString As String
  5.  
  6.  
  7. Set lst1 = Me!lstPickFrom
  8. Set lst2 = Me!lstPickTo
  9.  
  10. For Each itm In lst1.ItemsSelected
  11.    ' Set RowSource property for first selected item.
  12.    If lst2.RowSource = "" Then
  13.          lst2.RowSource = lst1.Column(0, itm) & ";" & lst1.Column(1, itm) & ";" & lst1.Column(2, itm)
  14.         Else
  15.             ' Check whether item has already been copied.
  16.              vSearchString = lst1.Column(0, itm) & ";" & lst1.Column(1, itm) & ";" & lst1.Column(2, itm)
  17.             If Not InStr(lst2.RowSource, vSearchString) > 0 Then
  18.                 lst2.RowSource = lst2.RowSource & ";" & lst1.Column(0, itm) & ";" & lst1.Column(1, itm) & ";" & lst1.Column(2, itm)
  19.  
  20.             End If
  21.         End If
  22.     Next itm
  23.  
Put this behind your button click event.

Thanks! That's exactly what I needed. However, I would still like to be able to see both of the columns that existed in my "selected from" list in my "selected to" list. I messed with changing the column size and count, but all that did was stack my selected items next to each other instead of showing both the UPC and the Item Description side by side as I had wanted. Here's what my code looks like that enables me to create multiple selections; maybe someone can help me figure out if it's possible to do what I'm asking. Also, I know this probably sounds stupid, but how do I preview a report based on the selections I've made? I installed a preview report button, but the report comes up blank when I click it. Hope the following code helps.

Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub cmdSelect1_Click()
  4.     Dim lst1 As ListBox, lst2 As ListBox
  5.     Dim itm As Variant
  6.  
  7.     Set lst1 = Me!lstProduct
  8.     Set lst2 = Me!lstSelected
  9.     ' Check selected items.
  10.     For Each itm In lst1.ItemsSelected
  11.         ' Set RowSource property for first selected item.
  12.         If lst2.RowSource = "" Then
  13.             lst2.RowSource = lst1.ItemData(itm)
  14.         Else
  15.             ' Check whether item has already been copied.
  16.             If Not InStr(lst2.RowSource, lst1.ItemData(itm)) > 0 Then
  17.                 lst2.RowSource = lst2.RowSource & ";" & lst1.ItemData(itm)
  18.             End If
  19.         End If
  20.     Next itm
  21. End Sub
  22.  
  23.  
  24. Private Sub cmdUnselect1_Click()
  25. Me!lstSelected.RowSource = ""
  26. Me!lstProduct.SetFocus
  27. Call Form_Load
  28. End Sub
  29.  
  30. Private Sub Form_Load()
  31.  
  32. DoCmd.MoveSize 2000, 1500
  33.  
  34. Dim lst1 As ListBox, lst2 As ListBox
  35. Dim strSQL As String, strMsg As String
  36.  
  37. Set lst1 = Me!lstProduct
  38. Set lst2 = Me!lstSelected
  39.  
  40. ' Initialize SQL string.
  41. strSQL = "SELECT [UPC],[ItemDescription] FROM tblProduct ORDER By UPC;"
  42.  
  43. ' Notify user if multiple selection is not enabled.
  44. If lst1.MultiSelect = 0 Then
  45.     'strMsg = "The MultiSelect property of List1 is set to None." & _
  46.     '"To enable multiple selection, open the form in Design view " _
  47.     '"and set the MultiSelect property to Simple or Extended."
  48.     MsgBox "Multiple Selection Not Enabled", vbInformation, "Multiple selection not enabled"
  49. End If
  50.  
  51. ' Fill List1.
  52. With lst1
  53.     .RowSourceType = "Table/Query"
  54.     .RowSource = strSQL
  55.     .ColumnCount = 2
  56. End With
  57. With lst2
  58. ' Specify that List2 is a value list.
  59.     .RowSourceType = "Value List"
  60.     .ColumnCount = 1
  61.     End With
  62. End Sub
  63. Private Sub cmdPreview_Click()
  64. On Error Resume Next
  65.     Dim stDocName As String
  66.     stDocName = "rptProduct"
  67.     DoCmd.OpenReport stDocName, acViewPreview
  68.     DoCmd.Maximize
  69. End Sub
By the way, I've decided I would just make one box invisible instead of trying to programmatically change the style of the box. It just seems easier to me. Thanks for the suggestion.
Feb 20 '08 #7
That works perfectly! Thank you.

Now, is there any way to preview a report based on the information that I have selected in my PickTo list? I currently have a preview report button, but it doesn't filter the report based on my selections. I'm sure it has something to do with an SQL statement, but as of yet, I haven't really messed with it.
Feb 20 '08 #8
mshmyob
904 Expert 512MB
You're welcome.

Answer to your report question. Yes. I will work on it and post later.

That works perfectly! Thank you.

Now, is there any way to preview a report based on the information that I have selected in my PickTo list? I currently have a preview report button, but it doesn't filter the report based on my selections. I'm sure it has something to do with an SQL statement, but as of yet, I haven't really messed with it.
Feb 20 '08 #9

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

Similar topics

3
by: Diane Yocom | last post by:
Has anybody ever seen or written code for ASP that would mimic Access' multi-column combo box? Specifically, I have a drop down box that lists about 100 five-digit codes. Each of these codes has...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
2
by: Warstar | last post by:
hi there i am working on a site with some forms and i was wondering is it possible to make a drop down menu where u can select like i want 1,2 or 3 new items and then the form would apear with 1,2...
14
by: J. Makela | last post by:
Hallo. This should be a pretty entertaining question for you *real* javascript writers.. I, being the lowly photoshop guy at an ad agency made the mistake of actually saying "HTML" in a...
5
by: Dan | last post by:
We have a simple site. It's a frameset with two frames a left and a right. The left frame is essentially a list of records from a database (using a server-side repeater control). When you click...
3
by: Brad Rogers | last post by:
All, Being immersed in vb.net and trying CSharp after almost a year I forgot the differences. I like vb fixing the uppercase/lowercase names and seeming to be more flexible to code entry. ...
11
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I know I sound like a one-note Johnny on this but I'm still looking for a solution. I need to display characters coming in from a serial port or a socket. I also need to be able to type...
10
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.