Hello,
I am struggling to grasp what exactly recordsets are? My problem is that I have a SQL statement which runs fine as a query, but when I run it with the doCmd.runSQL method, I get an error:
" Runtime error '2342': "
" A RunSQL action requires an argument consisting of"
" an SQL statement "
in an earlier thread : It was suggested to me that I use a recordset. I have read many similar threads but cannot get it to work.
Essentially all I would like to do is add an extra column/field to the table, and based on the value in the 'PostCode' field assign it a shortcode. The SQL query works fine by itself, the problem I have is executing it within VBA.
Any help/thoughts are greatly appreciated. Thanks!
I am struggling to grasp what exactly recordsets are? My problem is that I have a SQL statement which runs fine as a query, but when I run it with the doCmd.runSQL method, I get an error:
" Runtime error '2342': "
" A RunSQL action requires an argument consisting of"
" an SQL statement "
in an earlier thread : It was suggested to me that I use a recordset. I have read many similar threads but cannot get it to work.
Code:
Public Function generateStateColumn(tableName As String)
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb
Dim rs As DAO.Recordset
strSQL = " Select switch " & _
"(PostCode < 0900, 'NT', " & _
"PostCode < 2999, 'NSW', " & _
"PostCode < 3999, 'VIC', " & _
"PostCode < 4999, 'QLD', " & _
"PostCode < 5999, 'SA', " & _
"PostCode < 6999, 'WA', " & _
"PostCode < 7999, 'TAS', " & _
"PostCode > 7999, 'INVALID' ) " & _
"AS genSTATE " & _
"FROM " & tableName & " ;"
Set rs = CurrentDb.OpenRecordset(strSQL)
'*********** Something goes in here ??? *******
'*********** to make it work ??? **************
'If (Not rs.BOF And Not rs.EOF) Then
' rs.Fields("genSTATE").Value
'End If
'**********************************************
rs.Close
Set rs = Nothing
Debug.Print strSQL
End Function
Essentially all I would like to do is add an extra column/field to the table, and based on the value in the 'PostCode' field assign it a shortcode. The SQL query works fine by itself, the problem I have is executing it within VBA.
Any help/thoughts are greatly appreciated. Thanks!