VBA Code for a Combo box selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • t2bal
    New Member
    • Mar 2008
    • 4

    #1

    VBA Code for a Combo box selection

    Hi everyone, I'm really new to vba programming and need help.
    i have a combo box for hardware selection. in the list i have pc's, printers/scanners, laptops etc and I have also created and OK command button which when clicked after selecting hardware is supposed to return a form depending on the hardaware selected (each item in the list should be linked to a relevant form). And the RowSoureType is a "Value List".

    i have tried some code which doesn't seem to function as i wish coz it only returns the same form for all the items. What is the vba code that would work in this case?

    Thanks in advance
  • jeffstl
    Recognized Expert Contributor
    • Feb 2008
    • 432

    #2
    You need to post code so we can see what you are referring to exactly.

    Specifically to get the value of something selected in a text box you need to just do this:

    Code:
    Dim MySelectedValue as String
    
    MySelectedValue = Combo1.Text
    
    'Now MySelectedValue will hold the data from the drop down selection
    'But this code must be run either on a button click, or onchange of the combo
    Then you will need to be able to use that Value that you got to know which record to pull up from a database to load into your next form. This is why we probably need to see more to determine what you mean.

    You said you want to pull up a specific form, but really what you are probably doing is pulling up a form that loads data from a database based on what record you selected?

    Are you using SQL are just strictly binding controls?

    Are you not using a database at all and strictly pointing directly to seperately designed forms for each item in the drop down?

    Comment

    • t2bal
      New Member
      • Mar 2008
      • 4

      #3
      i don't know if i was supposed to write any other thing besides what u gave me but i just copied and pasted as u've povided it and now im getting a run time error saying 'object required'.

      and to answer some of your qstns, yes, im using a database and each value selected from the combo box should point directly to a separate form. So like u said, the value selected should then pull that specific form from the database.

      So this is the code that i had

      Private Sub cmdOK_Click()
      On Error GoTo Err_cmdOK_click

      Dim stDocName As String

      If stDocName = " Computer" & Me!cboHardwareS election.Column (1) Then
      DoCmd.OpenForm stDocName
      Else: stDocName = " Printer" & Me!cboHardwareS election.Column (2)
      DoCmd.OpenForm stDocName
      End If

      Exit_cmdOK_clic k:
      Exit Sub

      Err_cmdOK_click :
      MsgBox Err.Description
      Resume Exit_cmdOK_clic k
      End Sub

      hope this will help

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        Originally posted by t2bal
        i don't know if i was supposed to write any other thing besides what u gave me but i just copied and pasted as u've povided it and now im getting a run time error saying 'object required'.

        and to answer some of your qstns, yes, im using a database and each value selected from the combo box should point directly to a separate form. So like u said, the value selected should then pull that specific form from the database.

        So this is the code that i had

        Private Sub cmdOK_Click()
        On Error GoTo Err_cmdOK_click

        Dim stDocName As String

        If stDocName = " Computer" & Me!cboHardwareS election.Column (1) Then
        DoCmd.OpenForm stDocName
        Else: stDocName = " Printer" & Me!cboHardwareS election.Column (2)
        DoCmd.OpenForm stDocName
        End If

        Exit_cmdOK_clic k:
        Exit Sub

        Err_cmdOK_click :
        MsgBox Err.Description
        Resume Exit_cmdOK_clic k
        End Sub

        hope this will help
        I think I would now want to see what you have as values in those combo boxes. According to your code you have an actual form with the name "Computer+Whate verTheySelected inCombo" . Or atleast thats what the code will be looking for to open.

        If this is correct or what your trying to accomplish then as long as the text in the combo box matches the actual name of the form something like this should work I would think (doing something like this for both forms or however many forms, though you could reduce the number of if statements if you just included the "Printer" or "Computer" portions in the value of thier selection in the combo.

        Code:
        stDocName = " Computer" & Me!cboHardwareSelection.Text
        DoCmd.OpenForm stDocName

        Comment

        • t2bal
          New Member
          • Mar 2008
          • 4

          #5
          I have 3 items in my slection list, which are (PC's, Printers, Laptops). This combo box is on a form called frmHardwareSele ction. The problem with this code is that it opens only one form (which is the printers form) regardless of the selection. The code links all the Items to a single form although i thot the Else Statement would take care of that.

          Hope this will help and thanks 4ur support.

          Comment

          • jeffstl
            Recognized Expert Contributor
            • Feb 2008
            • 432

            #6
            Originally posted by t2bal
            I have 3 items in my slection list, which are (PC's, Printers, Laptops). This combo box is on a form called frmHardwareSele ction. The problem with this code is that it opens only one form (which is the printers form) regardless of the selection. The code links all the Items to a single form although i thot the Else Statement would take care of that.

            Hope this will help and thanks 4ur support.
            So it opens "the" printer form.......or does it open "a" form that LOADS printer information on to it?

            If there are seperate designed forms for EACH category in your combo box then use this. This will open the form based on the forms actual name only.

            Code:
            stDocName = " Computer" & Me!cboHardwareSelection.Text
            DoCmd.OpenForm stDocName
            If not then we need to look at the code on the form that needs to load so we can determine what that form is expecting when it opens.

            Comment

            • t2bal
              New Member
              • Mar 2008
              • 4

              #7
              yes it opens the printer form (and please note that all the forms were designed with the wizard).

              When i try to run this code (stDocName = " Computer" & Me!cboHardwareS election.Text
              DoCmd.OpenForm stDocName) it gives an error (runtime error 2185 - u can't reference a property or method for a control unless the control has a focus).

              And then when i use the same code with .Column(1) (which im not very sure what is supposed to) instead of .Text at the end, now it opens the computer's form for all the items(ie a single form for all the selection items just like it did with the printer form).

              Because a form opens with this code (which is fine) the issue then is to write a code to say; when the first item (Computers) is selected, open the computer form; and then when the second item (Printers) is selected, open the printer form; etc.

              i was actually trying to achive the above when i used the code that read;

              If stDocName = " Computer" & Me!cboHardwareS election.Column (1) Then
              DoCmd.OpenForm stDocName
              Else: stDocName = " Printer" & Me!cboHardwareS election.Column (2)
              DoCmd.OpenForm stDocName

              ...hoping that computer and printer forms will be opened accordingly depending on the selection from the combo box but it didn't work out that way and instead it only opened the printer form.

              Comment

              Working...