Hello all,
I have read many posts and am only slightly less confused than before. Could someone please give me a definitave answer as to wether or not SQL CASE statments can be used within VBA (Access) using the JET SQL engine?
I have a table in my db which I imported. This table contains a field 'PostCode' (zip code for the non australians).
With the knowledge that all Australian States have a specific postcode range, I would like to create a new field in the table, and enter a text value indicating the state.
Here is my code:
I realise I can do away with the 'BETWEEN' statements (assuming condition matches only in first instance), but they are not the problem (not at this stage...)
Is my CASE synax correct??
After reading the other posts suggesting that CASE statements were indeed not supported, I tried to use IFF statements. eg:
But I have no idea how to traverse for all instances, yet place the results into the singular 'genPOST' field.
Apologies for the extended ramble, I am very new to access and all things VBA, and the syntax and lack of documentation are driving me bananas
Thanks in advance!
I have read many posts and am only slightly less confused than before. Could someone please give me a definitave answer as to wether or not SQL CASE statments can be used within VBA (Access) using the JET SQL engine?
I have a table in my db which I imported. This table contains a field 'PostCode' (zip code for the non australians).
With the knowledge that all Australian States have a specific postcode range, I would like to create a new field in the table, and enter a text value indicating the state.
Here is my code:
Code:
Public Function generateStateColumn(tableName As String)
'tableName is the name of the table to which the field
'is to be added & populated
Dim db As DAO.Database
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb
'Generate a new column to store the generated state
'in
strSQL = "ALTER TABLE " & tableName & " " _
"ADD COLUMN genSTATE TEXT(5) ;"
DoCmd.RunSQL strSQL
' Now generate codes for the states
' NT : 0800 - 0899
' ACT : 2000 - 2999 -- Same as NSW so omit!
' NSW : 2000 - 2999
' VIC : 3000 - 3999
' QLD : 4000 - 4999
' SA : 5000 - 5999
' WA : 6000 - 6999
' TAS : 7000 - 7999
strSQL = "UPDATE " & tableName & " " & _
"CASE " & _
" WHEN Postcode BETWEEN 0800 AND 0899 THEN" & _
" SET genSTATE= 'NT' " & _
" WHEN Postcode BETWEEN 2000 AND 2999 THEN" & _
" SET genSTATE= 'NSW' " & _
.
.
.
" ELSE SET genSTATE = 'NULL' " & _
" END ;"
Debug.Print strSQL
DoCmd.RunSQL strSQL
End Function
I realise I can do away with the 'BETWEEN' statements (assuming condition matches only in first instance), but they are not the problem (not at this stage...)
Is my CASE synax correct??
After reading the other posts suggesting that CASE statements were indeed not supported, I tried to use IFF statements. eg:
Code:
strSQL = " Select " & _
"IIF(PostCode < 0900, NT, NULL) as genSTATE, " & _
"FROM " & tableName & " ;"
But I have no idea how to traverse for all instances, yet place the results into the singular 'genPOST' field.
Apologies for the extended ramble, I am very new to access and all things VBA, and the syntax and lack of documentation are driving me bananas
Thanks in advance!