PDA

View Full Version : Solved: Trying to create my first ComboBox



marshallgrad
12-01-2006, 11:53 PM
hello Gang,
I am new to the foruma and also new to the world of VBA.

We use a terminal emulator program at my workplace that has VB. I am trying to develope a Simple UserForm that has one ComboBox on it that will contain a list of names that I will be able to select from and populate the prompts of the program as i work. I am having trouble figuring out how to populate the ComboBox with the names. For simplicity sakes, lets assume that I want a static list of names (Enter the names one time and not have to worry about adding or deleting names on the fly.)

Here what I have done so far:

1. I opened up the VB application. The "About box" says that I am using the Microsoft Visual Basic ver 6.3. At the bottom it also says - Version 9972 VBA Retail 6.4.9972 Forms3 11.0.6550.

2. I Inserted a UserForm onto the work area and by default it was given the name of "UserForm1"

3. I inserted a Combobox onto the UserForm1 and it was given the default name of "ComboBox1".

4. I then inserted two commandbuttons onto the user form and they are named "CommandButton1" and "CommandButton2". (I have changed the caption property for the commandbuttons to say "OK" and "CANCEL")

Now I need the help. How do I enter code so that I can populate the ComboBox1 with names. For simplicity, lets say I want to enter two names, "Butch" and "Carol".

Thanks in advance for any help that is given,
Butch

marshallgrad
12-02-2006, 01:38 AM
Here is a pic of what I have so far.

Norie
12-02-2006, 01:45 AM
Where would you want to populate from?

Bob Phillips
12-02-2006, 08:47 AM
Private Sub UserForm_Initialize()
With Me.ComboBox1
.AddItem "Butch"
.AddItem "Carol"
End With
End Sub

marshallgrad
12-02-2006, 09:33 PM
Norie,

I think I understand your question. I would at first just like to populate the combobox as static data. I don't need to draw from any type of database or the like.

===================================================
Xld,

I tried what you have listed and it appears to work. Thanks very much.

marshallgrad
12-02-2006, 09:39 PM
Now for my next question. If I am using the above two names as my test case, How do I code for the name choosen in the combobox.

I take it I would want to put this under the commandbutton1 ("OK"). Something to the affect of;


Private Sub CommandButton1_Click()
CRITICAL_CALL_LIST.Hide
namechoosen = ComboBox1.Text
MsgBox namechoosen

End Sub



This seems to work. Remember I am taking baby steps with all of this...

Xld,
If I change the name of my UserForm1 to Critical_Call_List do I need to replace this in the above code you posted
Private Sub UserForm_Initialize() ' ==> Critical_Call_List_Initialize()
With Me.ComboBox1
.AddItem "Butch"
.AddItem "Carol"
End With
End Sub


Thanks again gang.
Butch

lucas
12-02-2006, 11:22 PM
you should unload CRITICAL_CALL_LIST instead of .hide if its a userform and you don't have a specific reason to just hide it.
unload CRITICAL_CALL_LIST
This should work for your commandbutton.....

Private Sub CommandButton1_Click()
CRITICAL_CALL_LIST.Hide
MsgBox ComboBox1.Text
End Sub


and

Private Sub UserForm_Initialize()

should not be changed. Just make sure its in the code for the userform.

Bob Phillips
12-03-2006, 01:55 PM
Xld,
If I change the name of my UserForm1 to Critical_Call_List do I need to replace this in the above code you posted
Private Sub UserForm_Initialize() ' ==> Critical_Call_List_Initialize()
With Me.ComboBox1
.AddItem "Butch"
.AddItem "Carol"
End With
End Sub

No you don't, it is the generic Userform, not the specific Userform1, that is it refers to the class.

marshallgrad
12-03-2006, 11:10 PM
Lucas & XLD,
Thanks. My limited testing is going pretty good.

I think I am ready to move onto the next big question. Norie had asked earlier in this thread where I wanted to populate from. Just to see if things would work, I wanted a static set of names. But moving onward I would like to eventually create a Combobox that would allow for the entry of new names, that, if not on the list, could be added. That would be my end goal.

I think right now, I need to determine where, based on my companies system, would be the best place to create a storage place for my list of names. I will try and describe what I am working with. My terminology and knowledge of systems (databases, etc) is limited, so bear with me.

We have a central server that contains a PUBLIC drive where documents and the like can be viewed by anyone who has access (access = a defined Username and Password to log onto the system). Each individual is also allocated a H:\ drive (for personal storage). Sitting on the desks in all the offices are individual desktop computers that have the C:\ drive availabl. Of course each person has his own user profile when looking under the C:\\documents and settings\"username".

How does one go about determining where to store this list of names if I want to be able to access the list from any of the desktops that I use. I take it that putting it someplace on the C:\ drive of one desktop is of no use.

Secondly, when we say database, what do we really mean? Are we talking a SQL application, or can this be something as simple as a NOTEPAD document with the names that I want to popluate the ComboBox1 list with:
example
==========================================
1. Butch Jones
2. Carol Jones
3. Frank Jones

Lets try and guide me on these points now and we can move to the next questions later.

thanks,
Butch

marshallgrad
12-04-2006, 04:38 AM
I also have one more question. Lets say that I have choosen a name in my combobox. I know that using "ComboBox1.Text" will capture the choosen name as a string. How do I pass that string to another section of code.

My Terminal Emulator that I am using has the ability to run Macro's.

Step 1: I choose my macro

Sub BUTCH_TEST_CRITICALVALUE()
' Generated by the Reflection Macro Recorder on 03-13-2004 05:34:47.90.
' Generated by Reflection for ReGIS Graphics 9.0.
On Error GoTo ErrorHandler
Const NEVER_TIME_OUT = 0
Dim LF As String ' Chr$(rcLF) = Chr$(10) = Control-J
Dim CR As String ' Chr$(rcCR) = Chr$(13) = Control-M
Dim CRITICALCOMMENT As String

LF = Chr$(rcLF)
CR = Chr$(rcCR)
With Session

BUTCH.Show

Exit Sub
ErrorHandler:
.MsgBox Err.Description, vbExclamation + vbOKOnly
End With
' Recording stopped at 05:34:56.75.
End Sub

This brings up the Userform of "BUTCH". This UserForm is the one with the Combobox on it and I select a name. I then combined the name with some other characters and want to pass this new string back to my original Macro so that I can transmit this new string to the Video display. how do I do this?

Private Sub CANCELBUTTON_Click()
Unload BUTCH

End Sub
Private Sub ComboBox1_Change()
' CHANGING THE COMBOBOX1 PROPERTY OF MATCHENTRY DOES THE FOLLOWING
' 0 = Basic matching. The control searches for the next entry that
' starts with the character entered. Repeatedly typing the same
' letter cycles through all entries beginning with that letter.
'
' 1 = Extended matching. As each character is typed, the control
' searches for an entry matching all characters entered (default).
'
' 2 = No Matching occurs.
End Sub
Public Sub OKBUTTON_Click()
Dim SHORTDATE As String
Dim MYDATE As String
Dim NAMECHOOSEN As String
Dim CRITICALCOMMENT As String
Unload BUTCH
NAMECHOOSEN = ComboBox1.Text
MYTIME = FormatDateTime(Now, 4)
MYDATE = FormatDateTime(Now, 2)
LENGTHOFDATE = Len(MYDATE) - 5
SHORTDATE = Mid(MYDATE, 1, LENGTHOFDATE)
CRITICALCOMMENT = "CVC " & NAMECHOOSEN & " " & SHORTDATE & "@" & MYTIME
'MsgBox "THE FULL DATE IS: " & MYDATE
'MsgBox "THE SHORT DATE IS: " & SHORTDATE
'MsgBox "THE TIME IS: " & MYTIME
'MsgBox "THIS IS THE ENTIRE OUTPUT: " _
'& vbCrLf & "CVC " & NAMECHOOSEN & " " & SHORTDATE & "@" & MYTIME
'X = ComboBox1.ListCount
'MsgBox "NAME = " & namechoosen
'MsgBox "NUMBER OF ITEMS IN LIST: " & X
'MsgBox "FIRST ITEM IN LIST: " & ComboBox1.List(0)
'MsgBox "SECOND ITEM IN LIST: " & ComboBox1.List(1)
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1

ComboBox1.AddItem "Butch JONES"
ComboBox1.AddItem "Carol JONES"

End With
End Sub

marshallgrad
12-05-2006, 09:07 AM
Not sure if I was clear in what I wanted from my above post.

I want to take the string value of CRITICALCOMMENT and pass it back to the original macro that called the userform. When I tried this on my own, the value was created as seen by my testing with the various msgbox diplays in the OKBUTTON_CLICK() event.

When it gets to the last part of that CLICK event, I assume the code then goes back to the SUB of BUTCH_TEST_CRITICALVALUE() but when I tried to test it with a msgbox, the value was "NULL" In reading up, I came across some reference about passing values to other subroutines, but didn't quite understand what was occuring.

I basically want my initial code to look like

Sub BUTCH_TEST_CRITICALVALUE()
' Generated by the Reflection Macro Recorder on 03-13-2004 05:34:47.90.
' Generated by Reflection for ReGIS Graphics 9.0.
On Error Goto ErrorHandler
Const NEVER_TIME_OUT = 0
Dim LF As String ' Chr$(rcLF) = Chr$(10) = Control-J
Dim CR As String ' Chr$(rcCR) = Chr$(13) = Control-M
Dim CRITICALCOMMENT As String

LF = Chr$(rcLF)
CR = Chr$(rcCR)
With Session

BUTCH.Show
.MsgBox CRITICALCOMMENT '<====>this is null when I test

Exit Sub
ErrorHandler:
.MsgBox Err.Description, vbExclamation + vbOKOnly
End With
' Recording stopped at 05:34:56.75.
End Sub

mdmackillop
12-05-2006, 01:48 PM
This will add names from a text file to the combobox

Option Explicit
Private Sub UserForm_Initialize()
Dim fs, a
'Open a txt file
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile("C:\AAA\MyNames.txt", 1, 0)
Do While a.AtEndOfStream <> True
'add each line to the combobox
Me.ComboBox1.AddItem a.ReadLine
Loop
a.Close
'Show first item
Me.ComboBox1.ListIndex = 0
End Sub

mdmackillop
12-05-2006, 02:08 PM
Here's as sample, using the text file as shown above.
I've added a Global Variable in Module1 where the ComboBox value can be stored, to be used by the calling or subsequent macros.

marshallgrad
12-07-2006, 03:49 PM
I didn't understand the last post. Is there a way to print text to the screen of the computer when you have your form up.

form1.show
whatever code goes here.........
<= how do you send data to the computer screen? What is code =>
form1.unload.

mdmackillop
12-07-2006, 04:23 PM
Try

Userform code:

Option Explicit
Private Sub UserForm_Initialize()
Dim fs, a
'Open a txt file
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile("C:\AAA\MyNames.txt", 1, 0)
Do While a.AtEndOfStream <> True
'add each line to the combobox
Me.ComboBox1.AddItem a.ReadLine
Loop
a.Close
End Sub
Private Sub ComboBox1_Click()
MyName = Me.ComboBox1
'Unload Butch
MsgBox "I selected " & MyName
End Sub



Module code:

Public MyName As String
Sub BUTCH_TEST_CRITICALVALUE()
' Generated by the Reflection Macro Recorder on 03-13-2004 05:34:47.90.
' Generated by Reflection for ReGIS Graphics 9.0.
On Error GoTo ErrorHandler
Const NEVER_TIME_OUT = 0
Dim LF As String ' Chr$(rcLF) = Chr$(10) = Control-J
Dim CR As String ' Chr$(rcCR) = Chr$(13) = Control-M
Dim CRITICALCOMMENT As String

LF = Chr$(rcLF)
CR = Chr$(rcCR)
With Session

Butch.Show


Exit Sub
ErrorHandler:
MsgBox Err.Description, vbExclamation + vbOKOnly
End With
' Recording stopped at 05:34:56.75.
End Sub

marshallgrad
12-08-2006, 04:33 AM
md,
I finally got the thing to work. Hurray...

Apparently I was not paying attention to my naming convention. Beginners mistake. The basic plan I envisioned is taking shape. I think I am going to close this thread for now and let me play around a while and try and digest the concepts that have been laid out here. I have been reading and can follow about half of what is going on. I need to experiment a little and then should be better suited to ask more questions in a few days/weeks..

Thanks for all that helped on this post. The concepts laid out were the key to keep me pointed in the right direction. My code may not be the most compact, but it works and isn't that the key to all of this...

Thanks again to all ,
Butch

Bob Phillips
12-09-2006, 10:33 AM
Always precede your code with

Option Explicit