Michael
Michael

Reputation: 3229

How to reference tables/queries/reports in access using an alias name?

In my Access database, I have tables with the naming convention of "t_customers" or "t_orders". I have a combo box which lets me pick from these options. For a better look, I want to have the options in the combo box without the "t_", so one of the options is just "customers". Then in VBA, I can access the combo box's value of "customers" and then pass that as a parameter to my function which will export the contents of the associated table "t_customers" to an excel file.

Is this possible to do? I can think of using a select case statement and for each combo box value I manually assign the related table value, but there has to be a better way. Is there some sort of short cut that does this in Access using a relationship or similar feature without coding? Or is there a simpler way to code this?

EDIT: I am also trying to get the value of the current item selected in my combo box named "cbTable". I have no macros attached to the combo box. I have a button that when pressed it runs a macro which calls my VBA function. In the function I have this code:

Dim cbValue As String
cbValue = cbTable.Value
MsgBox (cbValue)

I get this error:

Runtime Error '424'

Object Required

The error is on the

cbValue = cbTable.Value

line. What is wrong with this code and how do I get the current value of the combo box? Thanks in advance.

Upvotes: 0

Views: 6339

Answers (1)

Brad
Brad

Reputation: 12245

You can set your RowSource for your combobox to

SELECT MSysObjects.Name, Replace(MSysObjects.Name,"t_","") AS Expr1
FROM MSysObjects
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0));

Then set the Column Count to 2, the Column Widths to 0;2 (the second number just needs to be any positive number) an the Bound Column to 1.

Now when you go to call your function the value of the combobox will be the real name of the table, t-customers, but what you saw on your form was customers.


As for accessing that combobox a few things need to be in place.

Let's say you have a form, Form1, and your combobox, cbTable, is on that form. enter image description here

So when your form runs it looks like this

enter image description here

Now let's say you have an OnChange even for that combobox

enter image description here

Notice that when you type cbTable it appears in the intellisense (ctrl+space). This tells you that your object is accessible at this scope.

Now if you were in another module where that variable is out of scope you would get this error when you try to compile.

enter image description here

Because Module1 has no idea what cbTable is. However, you can reference anything so long as it's fully qualified. So if we run this test with your form open, and "orders" selected all 3 of these lines are equivalent

Forms("Form1").cbTable
Form_Form1!cbTable
Forms("Form1").Controls("cbTable")

enter image description here

Notice at the top of all my modules it says Option Explicit. This forces your code to basically be checked for syntax/scope validity before you run it. Tools > Options

enter image description here

Upvotes: 3

Related Questions