I dont understand how I can get this error?
object variable or with block variable not set.
I get this error when I am trying to add a new record.
can someone tell me what to check for in my code?
Sub SaveCurrentRecord()
Dim cmdCommand As ADODB.Command
Set cmdCommand = New ADODB.Command
Dim strSQL As String
If Not rsNames.BOF And Not rsNames.EOF Then
'create a new connection instance and open it using the connection string
Set cnDb = New ADODB.Connection
cnDb.Open strConnection
Dim intCurName As Integer
intCurName = 0
'if adding a new record
If blnAddMode = True Then
'create SQL to insert a new record into the database
'containing the values on the form
strSQL = "INSERT INTO tblName(" & _
"fldLastName, fldFirstName, fldTitle) " & _
"VALUES (" & _
"'" & Me.txtLastName & "', " & _
"'" & Me.txtFirstName & "', " & _
"'" & Me.txtTitle & "') "
Else
'create SQL to update the existing record in the
'database with the values on the form
strSQL = "UPDATE tblName SET " & _
"fldLastName = '" & Me.txtLastName & "', " & _
"fldFirstName = '" & Me.txtFirstName & "', " & _
"fldTitle = '" & Me.txtTitle & "', " & _
"WHERE fldNameId = " & rsNames!fldNameId
'save the id of the current record
intCurName = rsNames!fldNameId
End If
'set the command to the current connection
Set cmdCommand.ActiveConnection = cnDb
'set the insert or update SQL statement to the command text
cmdCommand.CommandText = strSQL
'execute the delete command against the database
cmdCommand.Execute
'while connected to the database, go ahead and
'repopulate the recordset to make sure it contains
'the most current values from the database.
Set rsNames.ActiveConnection = cnDb
rsNames.Requery
Set rsNames.ActiveConnection = Nothing
'move back to the contact that was current before the
'requery
If intCurName > 0 Then
'move back to the contact that was just updated
rsNames.Find "[fldNameId] = " & intCurName
Else
'if just added new record, move to the beginning of
'the recordset
rsNames.MoveFirst
End If
'reset add mode flag to false
blnAddMode = False
'populate the controls on the form
Call PopulateControlsOnForm
End If
End Sub
Private Sub btnAdd_Click()
Call clearControls
blnAddMode = True
End Sub
object variable or with block variable not set.
I get this error when I am trying to add a new record.
can someone tell me what to check for in my code?
Sub SaveCurrentRecord()
Dim cmdCommand As ADODB.Command
Set cmdCommand = New ADODB.Command
Dim strSQL As String
If Not rsNames.BOF And Not rsNames.EOF Then
'create a new connection instance and open it using the connection string
Set cnDb = New ADODB.Connection
cnDb.Open strConnection
Dim intCurName As Integer
intCurName = 0
'if adding a new record
If blnAddMode = True Then
'create SQL to insert a new record into the database
'containing the values on the form
strSQL = "INSERT INTO tblName(" & _
"fldLastName, fldFirstName, fldTitle) " & _
"VALUES (" & _
"'" & Me.txtLastName & "', " & _
"'" & Me.txtFirstName & "', " & _
"'" & Me.txtTitle & "') "
Else
'create SQL to update the existing record in the
'database with the values on the form
strSQL = "UPDATE tblName SET " & _
"fldLastName = '" & Me.txtLastName & "', " & _
"fldFirstName = '" & Me.txtFirstName & "', " & _
"fldTitle = '" & Me.txtTitle & "', " & _
"WHERE fldNameId = " & rsNames!fldNameId
'save the id of the current record
intCurName = rsNames!fldNameId
End If
'set the command to the current connection
Set cmdCommand.ActiveConnection = cnDb
'set the insert or update SQL statement to the command text
cmdCommand.CommandText = strSQL
'execute the delete command against the database
cmdCommand.Execute
'while connected to the database, go ahead and
'repopulate the recordset to make sure it contains
'the most current values from the database.
Set rsNames.ActiveConnection = cnDb
rsNames.Requery
Set rsNames.ActiveConnection = Nothing
'move back to the contact that was current before the
'requery
If intCurName > 0 Then
'move back to the contact that was just updated
rsNames.Find "[fldNameId] = " & intCurName
Else
'if just added new record, move to the beginning of
'the recordset
rsNames.MoveFirst
End If
'reset add mode flag to false
blnAddMode = False
'populate the controls on the form
Call PopulateControlsOnForm
End If
End Sub
Private Sub btnAdd_Click()
Call clearControls
blnAddMode = True
End Sub