Consulting

Results 1 to 17 of 17

Thread: Solved: Trying to create my first ComboBox

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location

    Solved: Trying to create my first ComboBox

    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

  2. #2
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location

    Here is pic

    Here is a pic of what I have so far.

  3. #3
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Where would you want to populate from?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    [vba]

    Private Sub UserForm_Initialize()
    With Me.ComboBox1
    .AddItem "Butch"
    .AddItem "Carol"
    End With
    End Sub
    [/vba]

  5. #5
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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;

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

    End Sub
    [/VBA]


    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
    [VBA]Private Sub UserForm_Initialize() ' ==> Critical_Call_List_Initialize()
    With Me.ComboBox1
    .AddItem "Butch"
    .AddItem "Carol"
    End With
    End Sub [/VBA]


    Thanks again gang.
    Butch

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    [vba]unload CRITICAL_CALL_LIST [/vba]
    This should work for your commandbutton.....
    [vba]
    Private Sub CommandButton1_Click()
    CRITICAL_CALL_LIST.Hide
    MsgBox ComboBox1.Text
    End Sub
    [/vba]

    and
    [vba]
    Private Sub UserForm_Initialize()
    [/vba]
    should not be changed. Just make sure its in the code for the userform.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Quote Originally Posted by marshallgrad
    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
    [vba]Private Sub UserForm_Initialize() ' ==> Critical_Call_List_Initialize()
    With Me.ComboBox1
    .AddItem "Butch"
    .AddItem "Carol"
    End With
    End Sub [/vba]
    No you don't, it is the generic Userform, not the specific Userform1, that is it refers to the class.

  9. #9
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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

  10. #10
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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

    [vba]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[/vba]

    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?

    [vba]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
    [/vba]
    Last edited by marshallgrad; 12-05-2006 at 12:26 AM.

  11. #11
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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

    [vba]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 [/vba]

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This will add names from a text file to the combobox
    [VBA]
    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

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    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.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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.

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try

    Userform code:
    [VBA]
    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

    [/VBA]

    Module code:
    [VBA]
    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

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  16. #16
    VBAX Regular
    Joined
    Dec 2006
    Location
    Kentucky
    Posts
    19
    Location
    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

  17. #17
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,443
    Location
    Always precede your code with

    Option Explicit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •