B
b99tw
I am new to VBA and am stumped by the error 3001 I'm getting for some data
values. I tried using the suggestions given for similar posts in this group,
but without success. Here is a summary of my problem: I'm using MSAccess
2007. From a form button click event, I am using the Find method to locate a
specific record in a table (cnaddres). The field name I want to search on is
Est Num; it is a text data type in the cnaddres table. I declared strSQL as
string variable and assigned it as strSQL = "[Est Num] = " & Me!EstNum (where
Me!EstNum is a current value from a form field). I then test for if EOF and
do stuff. Here's the strange part: The Find method works correctly when the
Est Num value starts with a number (e.g. 345p; 223; 45m6) However, I get the
3001 error message whenever the Est Num value starts with a non-digit
character (e.g. p99; F123).
At first I thought my problem had to do w/ SQL syntex for dealing with table
field names having spaces (e.g. Est Num), and I don't know SQL well. But I
don't think that's the problem since the Find method does work for some
values.
I've posted the code snippet below (please forgive my non-elegance...). I
would very very much appreciate any help on this. It is driving me a bit mad.
Thank you, Ellen
---------------
Private Sub bOpenBilling_Click()
'declare new connection object and instantiate new connection
Dim cnCurrent As ADODB.Connection
Set cnCurrent = CurrentProject.Connection
'declare new recordset and instantiate
Dim rsCNAddres As ADODB.Recordset
Set rsCNAddres = New ADODB.Recordset
Dim rsBillingActivity As ADODB.Recordset
Set rsBillingActivity = New ADODB.Recordset
'search for me.estNum and determine if found
Dim stDocName As String
Dim qry As String
Dim strSQL As String
Dim strEstNum As String
stDocName = "frmEnterBilling"
qry = "qrySortBillingActivityByCNDesc"
'search cnaddres for estnum
rsCNAddres.Open "cnaddres", cnCurrent, adOpenDynamic, adLockOptimistic,
adCmdTable 'point recordset to the CNAddres table
rsCNAddres.Filter = "" 'remove any filters
strSQL = "[Est Num] = " & Me!EstNum
rsCNAddres.Find strSQL 'THIS IS WHERE I GET THE 3001 ERROR FOR SOME VALUES
If rsCNAddres.EOF Then ' est num not found
'do stuff and exit Sub
Else 'est num found
'do stuff, insert new record into BillingActivity table, open Billing form
rsBillingActivity.Open "BillingActivity", cnCurrent, adOpenDynamic,
adLockOptimistic, adCmdTable 'point recordset to the BillingActivity table
rsBillingActivity.AddNew
rsBillingActivity!EstNum = strEstNum
rsBillingActivity.Update
DoCmd.OpenForm stDocName, WhereCondition:="[EstNum]=" & "'" & [EstNum]
& "'"
End If
Exit_bOpenBilling_Click:
Exit Sub
Err_bOpenBillinig_Click:
MsgBox Err.Description
Resume Exit_bOpenBilling_Click
rsCNAddres.Close 'close and destroy recordset
Set rsCNAddres = Nothing
rsBillingActivity.Close
Set rsBillingActivity = Nothing
End Sub
values. I tried using the suggestions given for similar posts in this group,
but without success. Here is a summary of my problem: I'm using MSAccess
2007. From a form button click event, I am using the Find method to locate a
specific record in a table (cnaddres). The field name I want to search on is
Est Num; it is a text data type in the cnaddres table. I declared strSQL as
string variable and assigned it as strSQL = "[Est Num] = " & Me!EstNum (where
Me!EstNum is a current value from a form field). I then test for if EOF and
do stuff. Here's the strange part: The Find method works correctly when the
Est Num value starts with a number (e.g. 345p; 223; 45m6) However, I get the
3001 error message whenever the Est Num value starts with a non-digit
character (e.g. p99; F123).
At first I thought my problem had to do w/ SQL syntex for dealing with table
field names having spaces (e.g. Est Num), and I don't know SQL well. But I
don't think that's the problem since the Find method does work for some
values.
I've posted the code snippet below (please forgive my non-elegance...). I
would very very much appreciate any help on this. It is driving me a bit mad.
Thank you, Ellen
---------------
Private Sub bOpenBilling_Click()
'declare new connection object and instantiate new connection
Dim cnCurrent As ADODB.Connection
Set cnCurrent = CurrentProject.Connection
'declare new recordset and instantiate
Dim rsCNAddres As ADODB.Recordset
Set rsCNAddres = New ADODB.Recordset
Dim rsBillingActivity As ADODB.Recordset
Set rsBillingActivity = New ADODB.Recordset
'search for me.estNum and determine if found
Dim stDocName As String
Dim qry As String
Dim strSQL As String
Dim strEstNum As String
stDocName = "frmEnterBilling"
qry = "qrySortBillingActivityByCNDesc"
'search cnaddres for estnum
rsCNAddres.Open "cnaddres", cnCurrent, adOpenDynamic, adLockOptimistic,
adCmdTable 'point recordset to the CNAddres table
rsCNAddres.Filter = "" 'remove any filters
strSQL = "[Est Num] = " & Me!EstNum
rsCNAddres.Find strSQL 'THIS IS WHERE I GET THE 3001 ERROR FOR SOME VALUES
If rsCNAddres.EOF Then ' est num not found
'do stuff and exit Sub
Else 'est num found
'do stuff, insert new record into BillingActivity table, open Billing form
rsBillingActivity.Open "BillingActivity", cnCurrent, adOpenDynamic,
adLockOptimistic, adCmdTable 'point recordset to the BillingActivity table
rsBillingActivity.AddNew
rsBillingActivity!EstNum = strEstNum
rsBillingActivity.Update
DoCmd.OpenForm stDocName, WhereCondition:="[EstNum]=" & "'" & [EstNum]
& "'"
End If
Exit_bOpenBilling_Click:
Exit Sub
Err_bOpenBillinig_Click:
MsgBox Err.Description
Resume Exit_bOpenBilling_Click
rsCNAddres.Close 'close and destroy recordset
Set rsCNAddres = Nothing
rsBillingActivity.Close
Set rsBillingActivity = Nothing
End Sub