VBA code didin't work anymore after upsized access 2007 to SQL Server 2005

jp2468

Registered User.
Local time
Today, 16:41
Joined
Sep 30, 2010
Messages
17
I have an Access 2007 application which has VBA code to avoid users to key in wrong data in the data entry form. After I upsized the main table to a back end SQL Server 2005, then link the main table at Access 2007 front end, the VBA code at Access 2007 didn’t work any more. All other functions are still OK.
Could anyone give me some ideals? I tried to use different SQL connection string, it still doesn’t work. The one of error message is “[Microsoft][ODBC SQL Server Diver ][SQL Server] Incorrect syntax near ‘!’.”
However, I didn't see anything wrong.
Thanks for your help
 
Could anyone give me some ideals?

Post the code. indicate which line causes the failure.
 
Lagbolt,

Thanks for reply!

The code is as the below:

Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
Dim strConnectString As String
 
strConnectString = "Provider=MSDASQL.1;" & _
"Data Source=MyDataSource; Initial Catalog=MyDatabaseName;" & _
"User ID=MyID; PWD=xxxx"
 
Set cnn = New ADODB.Connection
cnn.ConnectionString = strConnectString
cnn.Open
 
Set rst = cnn.Execute(" Select * from dbo.tblMyTable Where (dbo.tblMyTable.ServiceDate) = [Forms]![frmDataEntry]![ServiceDate]", Options:=adCmdText)

I am not really know which line causes the fuilure. According to the error message, looks like somethig wrong close to "!", but I don't know.

I am much apprecated if AWF VIP can help me.

Thanks a lot.
 
I expect that SQL Server doesn't know how to reference a value on your Access form, so it doesn't know what this is ...
Code:
[Forms]![frmDataEntry]![ServiceDate]
I don't know SQL Server but you might try ...
Code:
Set rst = cnn.Execute( _
  "Select * from dbo.tblMyTable " & _
  "Where (dbo.tblMyTable.ServiceDate) = " & [Forms]![frmDataEntry]![ServiceDate], _
    Options:=adCmdText)
... so we evaluate the object reference outside of the string. Do you see the difference?
Cheers,
Mark
 
Mark,

You are so smart, just made a little bit change, the error was gone. Thank you very much.

Now, go back to my main problem that VBA code didn't work anymore.
I copied my code below, would you please look at it, and give me your help? I looked code for a week, still cannot find a way to work out. If you can help me, I am really apprecated.
----------------------------
The code as below:

Function ToNextRecord() As Boolean
Dim req As Boolean
Dim varNum As Variant
' first check for required fields
req = False



'declars and instantiante one connection object and recordset object
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection

Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

Dim strConnectString As String

'strConnectStri\strConnectString = "Provider=MSDASQL.1;" & _
"Data Source=MydataSource; Initial Catalog=MyDatabase;" & _
"User ID=bb; PWD=xxxx"

Set cnn = New ADODB.Connection
cnn.ConnectionString = strConnectString
cnn.Open


Set rst = cnn.Execute( _
"Select * from dbo.tblMyTable " & _
"Where (dbo.tblMyTable.ServiceDate) = " & [Forms]![frmSession]![ServiceDate], _
Options:=adCmdText)

Do Until rst.EOF

' Finally check for overlap with existing request
' Try to grab ID - will be Null on unsaved row

varNum = Me.ID
If IsNull(varNum) Then varNum = 0 ' Set dummy value
If Not (IsNull(Me.CmbClient) Or IsNull(Me.ServiceDate) Or IsNull(Me.StartTime) Or IsNull(Me.EndTime)) Then

If Not IsNull((rst!ClientID = Me.CmbClient) And (rst!ServiceDate = Me.ServiceDate) And (rst!StartTime < Me.EndTime) And (rst!EndTime > Me.StartTime) And (rst!ID <> _
varNum) And (rst!Flag2 Is Null) And (Forms!frmSession!CmbFlag2 Is Null)) Then
MsgBox "Database already has a Service that overlaps the Service Time you have entered. Please correct the errors first!", vbCritical, "Service times is not correct"
'vbQuestion vbYesNo
'Cancel = True
StartTime.SetFocus
req = True
'Exit Sub
'End If
End If
Else
MsgBox "Please fill the required fields", vbCritical, "complete data entry form"
End If

rst.MoveNext
Loop

If req = False Then
ToNextRecord = True
Else
ToNextRecord = False
End If

'close and destroy the recordset object and the connecting object
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing
End Function
 
Understanding someone else's code is not trivial. Please describe a problem in greater detail than to say, "this code doesn't work." What doesn't work about it? Where doesn't it work? More details please.
Also, to offset your code and preserve indents, wrap it in code tags. Highlight a bunch of text and hit the '#' character in the toolbar. Makes your code much easier to read
Mark
 
Mark,

Thanks for reply. I should make code clearly.

The Orginal data entry form at Access 2007 alllow users to key in service with begin time and end time. Every time when a use key in a new record, the VBA code compare it with the records in the main table. if there is a overlapped service time, the data entry form will have message to let user correct error, and don't allow them to key in overlapped times.

After the main table upsized to SQL servere, you still can key in each service to database, however, the VBA code used to catch overlapped service time didn't work anymore. You can key in any overlapped times in database.

Any reports, search functions are still work.

The code has problem as the below:

Code:
Do Until rst.EOF
 
' Finally check for overlap with existing request
' Try to grab ID - will be Null on unsaved row
 
varNum = Me.ID
If IsNull(varNum) Then varNum = 0 ' Set dummy value
  If Not (IsNull(Me.CmbClient) Or IsNull(Me.ServiceDate) Or IsNull    (Me.StartTime) Or IsNull(Me.EndTime)) Then
 
   If Not IsNull((rst!ClientID = Me.CmbClient) And (rst!ServiceDate =  Me.ServiceDate) And (rst!StartTime < Me.EndTime) And (rst!EndTime > Me.StartTime) And (rst!ID <> _
varNum) Then
MsgBox "[URL="http://www.access-programmers.co.uk/forums/showthread.php?p=1091347#"][COLOR=darkgreen]Database[/COLOR][/URL] already has a Service that overlaps the Service Time you have entered. Please correct the errors first!", vbCritical, "Service times is not correct"
StartTime.SetFocus
req = True
'Exit Sub
'End If
End If
Else
MsgBox "Please fill the required fields", vbCritical, "complete data entry form"
End If
 
rst.MoveNext
Loop
 
Doesn't this logic...
Code:
(rst!StartTime < Me.EndTime) And (rst!EndTime > Me.StartTime)
...require an OR statement? In fact, don't you have an overlap if one of these is true? But with the AND in there the expression is only true if both start AND end overlap. Does that make sense?
And for what it's worth, this, if this is the problem, does not appear to be the result of an upsize.
But is that it?
Cheers,
Mark
 
Mark,

I am so glad you always give me good suggestions. It does make sense to change to OR.

However, even I use AND, it works at Access 2007 to catch overlapped times.

After upsized to SQL 2005, that function didn't work anymore. Even I added SQL connections, and recordset, it still didn't work.

Please help.
 
It's irrelevant if it used to work in a different system in a different way. We need to get it working now. Have you changed the SQL as per the previous post? If so and it still fails, please describe the failure in detail and post all code and SQL that expose the specifics of the problem.
Like, to say 'it doesn't work' is not enough. How does it fail? Where? And what happens?
 

Users who are viewing this thread

Back
Top Bottom