N
Number Cruncher
I want to use a VBA function that is a variable that references values in a
table is Access. I want to use the function in the normal query design of
Access. So, in the normal query designer, I would use as one of the columns
something like “NewCol:[NbrFromTblData]*2â€.
I have tried the following code and different variations of it. When I use
the following code and use the function in the regular Access query, the
value of the first record in the table shows up for every record in the query
results. I want the unique value from each record to show up in the results.
As much as I have tried different things, I am starting to wonder if it is
even possible to use a VBA function that references unique values in the
various rows of a table – to use it in a regular Access query. I know that
in my above example, I could skip VBA and put it all in the regular Access
query, but, I have complex situations where I think it would be much more
efficient using VBA code. Instead of doing nested if’s in the query, I would
rather use “Case†in VBA code – especially since in some situations, I have
twelve different “Casesâ€.
Here is the code I am trying:
Public Function TryVar() As Variant
Dim db As DAO.Database
Dim tblMyTbl As DAO.TableDef
Dim rst As DAO.Recordset
Dim fldMyField As DAO.Field
Set db = CurrentDb
Set tblMyTbl = db.TableDefs![acctcodes]
Set rst = db.OpenRecordset("acctcodes")
TryVar2 = rst![NbrFromTblData] ' In Access query,
' each row shows only the number in the first row
' I want the unique number in each row.
End Function
table is Access. I want to use the function in the normal query design of
Access. So, in the normal query designer, I would use as one of the columns
something like “NewCol:[NbrFromTblData]*2â€.
I have tried the following code and different variations of it. When I use
the following code and use the function in the regular Access query, the
value of the first record in the table shows up for every record in the query
results. I want the unique value from each record to show up in the results.
As much as I have tried different things, I am starting to wonder if it is
even possible to use a VBA function that references unique values in the
various rows of a table – to use it in a regular Access query. I know that
in my above example, I could skip VBA and put it all in the regular Access
query, but, I have complex situations where I think it would be much more
efficient using VBA code. Instead of doing nested if’s in the query, I would
rather use “Case†in VBA code – especially since in some situations, I have
twelve different “Casesâ€.
Here is the code I am trying:
Public Function TryVar() As Variant
Dim db As DAO.Database
Dim tblMyTbl As DAO.TableDef
Dim rst As DAO.Recordset
Dim fldMyField As DAO.Field
Set db = CurrentDb
Set tblMyTbl = db.TableDefs![acctcodes]
Set rst = db.OpenRecordset("acctcodes")
TryVar2 = rst![NbrFromTblData] ' In Access query,
' each row shows only the number in the first row
' I want the unique number in each row.
End Function