473,378 Members | 1,439 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,378 software developers and data experts.

VBA Code for a Combo box selection

4
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
Mar 10 '08 #1
6 2227
jeffstl
432 Expert 256MB
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:

Expand|Select|Wrap|Line Numbers
  1. Dim MySelectedValue as String
  2.  
  3. MySelectedValue = Combo1.Text
  4.  
  5. 'Now MySelectedValue will hold the data from the drop down selection
  6. 'But this code must be run either on a button click, or onchange of the combo 
  7.  
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?
Mar 10 '08 #2
t2bal
4
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!cboHardwareSelection.Column(1) Then
DoCmd.OpenForm stDocName
Else: stDocName = " Printer" & Me!cboHardwareSelection.Column(2)
DoCmd.OpenForm stDocName
End If

Exit_cmdOK_click:
Exit Sub

Err_cmdOK_click:
MsgBox Err.Description
Resume Exit_cmdOK_click
End Sub

hope this will help
Mar 11 '08 #3
jeffstl
432 Expert 256MB
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!cboHardwareSelection.Column(1) Then
DoCmd.OpenForm stDocName
Else: stDocName = " Printer" & Me!cboHardwareSelection.Column(2)
DoCmd.OpenForm stDocName
End If

Exit_cmdOK_click:
Exit Sub

Err_cmdOK_click:
MsgBox Err.Description
Resume Exit_cmdOK_click
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+WhateverTheySelectedinCombo" . 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.

Expand|Select|Wrap|Line Numbers
  1. stDocName = " Computer" & Me!cboHardwareSelection.Text
  2. DoCmd.OpenForm stDocName
  3.  
Mar 11 '08 #4
t2bal
4
I have 3 items in my slection list, which are (PC's, Printers, Laptops). This combo box is on a form called frmHardwareSelection. 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.
Mar 12 '08 #5
jeffstl
432 Expert 256MB
I have 3 items in my slection list, which are (PC's, Printers, Laptops). This combo box is on a form called frmHardwareSelection. 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.

Expand|Select|Wrap|Line Numbers
  1. stDocName = " Computer" & Me!cboHardwareSelection.Text
  2. DoCmd.OpenForm stDocName
  3.  
  4.  
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.
Mar 12 '08 #6
t2bal
4
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!cboHardwareSelection.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!cboHardwareSelection.Column(1) Then
DoCmd.OpenForm stDocName
Else: stDocName = " Printer" & Me!cboHardwareSelection.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.
Mar 13 '08 #7

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

Similar topics

2
by: Tim Blackwell | last post by:
i have some php code pulling some data from a database and i need to access some of hte variables in java script. i've never done this before. how can i do it. eg. i'm loading a combo box...
1
by: TNG | last post by:
Hi, Question, I use often a combo box on my ASP pages. each time a form is submitted, the value of the combo box returns to the first selection But is there a way to avoid this and show always...
0
by: Josh Nikle | last post by:
I just realized that I posted this earlier with no title. Sorry. Here's the original post: I have a form with a combo box on it that, when a selection is made, fills in several other controls...
9
by: Bob Alston | last post by:
In 2002, "GrayJay" posted the following code: I did this in a jazz record catalogue to find composers - On a form "frmComposers" Create a text box - txtFindComposer, and add the following sub...
1
by: BigJay | last post by:
I am 1. trying to have a combobox used as a selector to display records in a subform. and not sure on how to get selected info into subform.the combo is populated but can not get subform updated...
4
by: meganrobertson22 | last post by:
Hi Everyone- I have a question about how to add and then use the "All" selection in a combo box. I am trying to figure out how to: (1) add "All" as a selection to a combo box and then (2)...
7
by: Doug | last post by:
Hi I have a combo box (A) that populates a following combo box (B) based on a selection. The selection from the first combo box (A) initiates an OleDbDataAdapter routine that extracts the...
3
by: hmiller | last post by:
Hey everyone, I am having a hell of a time trying to set this menu system up. Here's what I'm trying to do. Combo Box One; is populated by names under properties "row source" "Phase 1"...
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
1
by: didihynes | last post by:
Hi Guys, I'm in desparate need of help. I am producing a database for my dissertation and have got majorly stuck. I am currently creating a form in which the user will select a student from a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.