Full Version: Newbie Question - Need Help Making A Button That Can Choose Between 2 Forms
UtterAccess Discussion Forums > Microsoft® Access > Access Macros
garethjones1
Hello, I'm very new with Macros. But I've got a small problem that I think wouldn't be much of a problem for someone with a little more experance with the subject.

I have a form, It asks for name, address and other details. At the bottom of the page i've got a combo box that has two options,

1. database 1
2. database 2

I've also got a button, I would like the button to open up Database 1 if that one is selected in the combo box, Or database 2 of database 2 is selected.

I put in a button and used to expression builder to setup the macros as follows,

Condition Action
[Forms]![Test Form 1]![database]="Database1" Open form
[Forms]![Test Form 2]![database]="Database2" Open form

When I run this i get a 'Type mismatch' error.
When i remove the conditions it opend up both database1 and 2 when i press the button.

I've been searching online for any help for the last few hours and it's very annoying as i can't seem to find anything that can help,

Thanks for your time,

Gareth
Bob G
welcome2UA.gif

we will probably need to know more about the combobox but it would be something like this aircode.

CODE
docmd.openform [Forms]![Test Form 1]![database]

doctor9
Gareth,

welcome2UA.gif

If you'd be willing to use VBA instead of macros, I think a solution would be pretty straightforward.

First, I'd like to address a potential problem: It appears that either your form or your combobox is named "Database", based on your posted macro code. If that's true, you should change this right away, as "Database" is a reserved word in Access. Your forms, reports and controls should all have meaningful names, so it's easier to tell what they do. This is very important especially if someone else ever needs to maintain your database in the future.

For example, a good name for your form could be "frmUserData", since it asks for names and addresses. A good name for your combobox could be something like "cmbSelectedDatabase" - where "cmb" indicates that it's a combo box, and the user is selecting a database.

As to the actual VBA code, you could use something alone the lines of this in your command button's Click Event code:

CODE
Private Sub OpenSelectedForm_Click()

    Select Case Me.cmbSelectedDatabase
    Case "Database1"
        DoCmd.OpenForm "Test Form 1"
    Case "Database2"
        DoCmd.OpenForm "Test Form 2"
    End Select
    
End Sub


In this example, if the user selects "Database1" in the combobox, and then presses the command button, the form named "Test Form 1" opens. If the user selects "Database2", the form named "Test Form 2" opens.

Basically, the "Select Case" structure lets you easily add new options to the list of possible choices if you ever need to. Since your user is using a combobox to select either "Database1" or "Database2", I assumed it would be likely/possible that you might add more choices down the road. If that's true, this code is ideal for you. If you were to add "Database 3" to your combobox, you would just add the following code to the above sample, just above the "End Select" bit:

CODE
    Case "Database3"
        DoCmd.OpenForm "Test Form 3"


Hope this helps,

Dennis
theDBguy
Hi Gareth,

welcome2UA.gif

What version of Access are you using? Please remember to select the version number when posting questions in case it becomes relevant to the discussion.

Can you post the SQL for the Row Source of your Combobox? If you are hiding the ID field, then that's what you want to use in your macro. Also, what is the name of the form with the combobox? Your sample code seems to point to two different forms.

Just my 2 cents... 2cents.gif
garethjones1
Thanks alot for all your help, I think i'll try the VBA route. I've renamed the combo box to a generic name (didn't know Database was reserved). I'll need some time to work out exactly how to use VBA. I've done some coding in the past but nothing for the last 10 years.

Thanks again for all your help, and this has put me in the right direction to fix the problem.

Thanks,

Gareth.

PS, If anyone knows any good books/ebooks/sites that could help a novice?
doctor9
Gareth,

One of our cooler contributors, Crystal, has a website that should be able to help you out.

Dennis
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.