BrokenBiker
ManicMechanic
- Local time
- Today, 14:08
- Joined
- Mar 22, 2006
- Messages
- 128
I was following this thread http://www.access-programmers.co.uk/forums/showthread.php?t=91128&highlight=append+query+vba and tried to implement that code, but no luck. I know there are parts of the code that I don't understand, and I need some schoolin'.
The intent is to have the DestinationDB/file directory built into the VBA qry as a variable.
As I have it now, I get a run-time error 2465: can't find the field 'l' refered to in your expression. (That's probably because that's part of the code that I'm having trouble understanding exactly what's needed there.)
This is the code as I have it loaded in my form. The de-bugger highlights the line in red. I put my understanding/questions of the code next to the portions that I'm confused on, along with the original code.
________________________
Dim qdf As QueryDef
Dim varDatabaseName As Variant
varDatabaseName = DLookup([Me.Text1], "Main Table", "[*] = '" & Me.Text1 & "'")Below is the original code--
varDatabaseName = DLookUp("[ServerPath]", "SomeTable", "[SomeField] = '" & Me.SomeCriteria & "'")
--I assume the ServerPath is variable, SomeTable is the table you want to append to, but what's [SomeField]? Do I need this code for each field in the table?
If IsNull(varServer) Then
MsgBox ("Unknown directory.")
Else
strSQL = "INSERT INTO Main Table (*) " & _
"IN '" & varDatabaseName & "' " & _
"SELECT Main_Table.* " & _
"FROM [Main Table] " & _
"WHERE [Main Table].Date <Now()"
'I tried to replace all of the field-specific lines w/ *. Will this work?Below is the original code for this portion.
strSQL = "INSERT INTO Customers (Advisor, ClientID, C1surname, " & _
" C1firstname, C2Surname, C2firstname, " & _
" stNo, StName, Locality, postcode) " & _
"IN '" & varDatabaseName & "' " & _
"SELECT advisors, ClientNo, Client1Name1, Client1Name2, Client2Name1, Client2Name2, StreetNo, StreetName, Locality, Postcode " & _
"FROM [Client action] " & _
"WHERE [Client action].ClientNo = " & [forms]![client action amended]![clientNo] & ";"
Set qdf = CurrentDb.QueryDefs("qryMainTblAppend")
qdf.SQL = strSQL
qdf.Close
Below is the original code for this portion.
End If
Set qdf = CurrentDb.QueryDefs("SomeReusableQuery")
qdf.SQL = strSQL
qdf.Close
________________________
Attached are two DBs, TestX and TestArchiveX. Open the TestX, and click on the button to run the code. It should append the Main Table in TestX to the Main Table in TestArchiveX.
The intent is to have the DestinationDB/file directory built into the VBA qry as a variable.
As I have it now, I get a run-time error 2465: can't find the field 'l' refered to in your expression. (That's probably because that's part of the code that I'm having trouble understanding exactly what's needed there.)
This is the code as I have it loaded in my form. The de-bugger highlights the line in red. I put my understanding/questions of the code next to the portions that I'm confused on, along with the original code.
________________________
Dim qdf As QueryDef
Dim varDatabaseName As Variant
varDatabaseName = DLookup([Me.Text1], "Main Table", "[*] = '" & Me.Text1 & "'")Below is the original code--
varDatabaseName = DLookUp("[ServerPath]", "SomeTable", "[SomeField] = '" & Me.SomeCriteria & "'")
--I assume the ServerPath is variable, SomeTable is the table you want to append to, but what's [SomeField]? Do I need this code for each field in the table?
If IsNull(varServer) Then
MsgBox ("Unknown directory.")
Else
strSQL = "INSERT INTO Main Table (*) " & _
"IN '" & varDatabaseName & "' " & _
"SELECT Main_Table.* " & _
"FROM [Main Table] " & _
"WHERE [Main Table].Date <Now()"
'I tried to replace all of the field-specific lines w/ *. Will this work?Below is the original code for this portion.
strSQL = "INSERT INTO Customers (Advisor, ClientID, C1surname, " & _
" C1firstname, C2Surname, C2firstname, " & _
" stNo, StName, Locality, postcode) " & _
"IN '" & varDatabaseName & "' " & _
"SELECT advisors, ClientNo, Client1Name1, Client1Name2, Client2Name1, Client2Name2, StreetNo, StreetName, Locality, Postcode " & _
"FROM [Client action] " & _
"WHERE [Client action].ClientNo = " & [forms]![client action amended]![clientNo] & ";"
Set qdf = CurrentDb.QueryDefs("qryMainTblAppend")
qdf.SQL = strSQL
qdf.Close
Below is the original code for this portion.
End If
Set qdf = CurrentDb.QueryDefs("SomeReusableQuery")
qdf.SQL = strSQL
qdf.Close
________________________
Attached are two DBs, TestX and TestArchiveX. Open the TestX, and click on the button to run the code. It should append the Main Table in TestX to the Main Table in TestArchiveX.