MS Access VBA Scripts that create SQL Statements:
I have a SQL Statement that derives two fields in a new table SATable:
SQLScript = "SELECT [DATA OUTPUT TABLE].* INTO SATable"
SQLScript = SQLScript & " FROM [DATA OUTPUT TABLE]"
SQLScript = SQLScript & " WHERE ((([DATA OUTPUT TABLE].[Journal Voucher ID]) In (SELECT [DATA OUTPUT TABLE].[Journal Voucher ID]"
SQLScript = SQLScript & " FROM [DATA OUTPUT TABLE]"
SQLScript = SQLScript & " GROUP BY [DATA OUTPUT TABLE].[Journal Voucher ID]"
SQLScript = SQLScript & " HAVING (((First([DATA OUTPUT TABLE].dBranch))=" & Chr(34) & "Navy" & Chr(34) & ") AND ((Last([DATA OUTPUT TABLE].dBranch))=" & Chr(34) & "USMC" & Chr(34) & ")) OR (((First([DATA OUTPUT TABLE].dBranch))=" & Chr(34) & "USMC" & Chr(34) & ") AND ((Last([DATA OUTPUT TABLE].dBranch))=" & Chr(34) & "Navy" & Chr(34) & "));)));"
DoCmd.RunSQL SQLScript
Then I have a statement that takes those two derived fields and appends them onto the original table:
DoCmd.RunSQL "UPDATE SATable INNER JOIN [DATA OUTPUT TABLE] ON (SATable.[Journal Voucher ID] = [DATA OUTPUT TABLE].[Journal Voucher ID]) AND (SATable.dBranch = [DATA OUTPUT TABLE].dBranch) SET [DATA OUTPUT TABLE].[Shared Appropriations] = [SATable].[dBranch] & " & Chr(34) & " Shared Appropriation" & Chr(34) & ";"
My goal is to remove the SATable and perform this action in one statement.
I've struggled with problems like this before so instead of just providing an answer can you please provide some explanation - TEACH A MAN TO FISH!
Cheers, -E
EDIT:------------------------------------------------------------- The Data Output Table has:
JVID dBranch SA
1 Navy N/A
2 Navy N/A
3 Navy N/A
4 Navy N/A
A USMC N/A
B USMC N/A
3 USMC N/A
4 USMC N/A
So the SATable becomes:
JVID dBranch SA
3 Navy Navy SA
3 USMC USMC SA
4 Navy Navy SA
4 USMC USMC SA
Then the update query updates the SA field, which is N/A by default
JVID dBranch SA
1 Navy N/A
2 Navy N/A
3 Navy Navy SA
4 Navy Navy SA
A USMC N/A
B USMC N/A
3 USMC USMC SA
4 USMC USMC SA