Hello all,
I am extremely new to VBA for Access. I have a series of tables (one for each year), all of which I want to query in the same way and then combine all the queries into one table using a union query. I have constructed some code which will do exactly this which is great. The problem I have is that the older years tables don't have all the fields that the later years do. Therefore when I run the code I get roughly 30 'Enter Parameter Value' prompt boxes. I need to enter "null" into all of them. Is there any way I can incorporate some override into my code which will automatically enter "null" into the box? I don't want to disable it since I don't want blank cells.
Here is a simplified version of my code:
Sub Loop_Criteria_UnionQUERY()
'define names for the database, query table and the SQL string
Dim db As Database
Dim qdf As QueryDef
Dim def As TableDef
Dim strName As String
Dim strSQLcriteriaquery As String
Dim strUnionQuery As String
'look into the current database
Set db = CurrentDb
'Loop through all tables in the file.
For Each def In db.TableDefs
'This if statement ensures the inbuilt tables are not affected.
If Left(def.Name, 4) <> "MSys" Then
strName = def.Name & "Query"
strSQLcriteriaquery = "SELECT [" & def.Name & "].[SEX], [" & def.Name & "].[AGE] WHERE ((([" & def.Name & "].[SEX])='M'));"
strUnionQuery = strUnionQuery & Left(strSQLcriteriaquery, (Len(strSQLcriteriaquery) - 1)) & " UNION ALL "
'Create a new query: CreateQueryDef("name to assign to the query table", SQLstringname)
Set qdf = db.CreateQueryDef(strName, strSQLcriteriaquery)
'Open the new query
DoCmd.OpenQuery strName
End If
Next def
strUnionQuery = Left(strUnionQuery, (Len(strUnionQuery) - 10)) & ";"
Set qdf = db.CreateQueryDef("UnionQuery", strUnionQuery)
DoCmd.OpenQuery "UnionQuery"
End Sub
Any help appreciated.
Many thanks,
D
I am extremely new to VBA for Access. I have a series of tables (one for each year), all of which I want to query in the same way and then combine all the queries into one table using a union query. I have constructed some code which will do exactly this which is great. The problem I have is that the older years tables don't have all the fields that the later years do. Therefore when I run the code I get roughly 30 'Enter Parameter Value' prompt boxes. I need to enter "null" into all of them. Is there any way I can incorporate some override into my code which will automatically enter "null" into the box? I don't want to disable it since I don't want blank cells.
Here is a simplified version of my code:
Sub Loop_Criteria_UnionQUERY()
'define names for the database, query table and the SQL string
Dim db As Database
Dim qdf As QueryDef
Dim def As TableDef
Dim strName As String
Dim strSQLcriteriaquery As String
Dim strUnionQuery As String
'look into the current database
Set db = CurrentDb
'Loop through all tables in the file.
For Each def In db.TableDefs
'This if statement ensures the inbuilt tables are not affected.
If Left(def.Name, 4) <> "MSys" Then
strName = def.Name & "Query"
strSQLcriteriaquery = "SELECT [" & def.Name & "].[SEX], [" & def.Name & "].[AGE] WHERE ((([" & def.Name & "].[SEX])='M'));"
strUnionQuery = strUnionQuery & Left(strSQLcriteriaquery, (Len(strSQLcriteriaquery) - 1)) & " UNION ALL "
'Create a new query: CreateQueryDef("name to assign to the query table", SQLstringname)
Set qdf = db.CreateQueryDef(strName, strSQLcriteriaquery)
'Open the new query
DoCmd.OpenQuery strName
End If
Next def
strUnionQuery = Left(strUnionQuery, (Len(strUnionQuery) - 10)) & ";"
Set qdf = db.CreateQueryDef("UnionQuery", strUnionQuery)
DoCmd.OpenQuery "UnionQuery"
End Sub
Any help appreciated.
Many thanks,
D