Question : Object variable or with block variable not set

i m using access and i have the above error

Dim DB As Database
Dim rs As DAO.Recordset
For i = Val(txtNoFrom) To Val(txtNoTO)
cmbCarInsert.SetFocus

     DB.Execute "Insert into tblPetrolDetails (PetrolID, Carno, voucherno,vouchervalue ) " & _
" values (" & ID & ",'" & cmbCarInsert.Text & "','" & i & "'," & Val(txtInsertValue) & ") "
     
Next

The breakpoint is at db.execute at the very first round of the loop. i checked all the values of the control, all are not empty.

Answer : Object variable or with block variable not set

Dim DB As Database
Dim rs As DAO.Recordset

set DB=currentdb()   '<<< ADD this line

For i = Val(txtNoFrom) To Val(txtNoTO)
cmbCarInsert.SetFocus

     DB.Execute "Insert into tblPetrolDetails (PetrolID, Carno, voucherno,vouchervalue ) " & _
" values (" & ID & ",'" & cmbCarInsert.Text & "','" & i & "'," & Val(txtInsertValue) & ") "
     
Next

Random Solutions  
 
programming4us programming4us