Hi,
I have the following code to feed data to two related tables. The mother table is tblInvoice and the child is invoicejoin, Therefore I cant feed data to the child before feeding to the mother table. The mother table is to be fed with data from specific textboxes in my form while the child table receives data from the listbox in my form.
The code below saves data to the mother table but brings a run time error message saying OBJECT VARIABLE OR WITH BLOCK VARIABLE NOT SET. Kind help me solve this error.
I have the following code to feed data to two related tables. The mother table is tblInvoice and the child is invoicejoin, Therefore I cant feed data to the child before feeding to the mother table. The mother table is to be fed with data from specific textboxes in my form while the child table receives data from the listbox in my form.
The code below saves data to the mother table but brings a run time error message saying OBJECT VARIABLE OR WITH BLOCK VARIABLE NOT SET. Kind help me solve this error.
Code:
Option Compare Database
Option Explicit
Dim Db As Database
Dim rs As New ADODB.Recordset
Dim rs2 As New ADODB.Recordset
Dim rs3 As New ADODB.Recordset
Dim cn As New ADODB.Connection
Dim cnb As New ADODB.Connection
Dim sql As String
Dim ID As Long
Dim Status As String
Dim Image As Office.FileDialog
Dim ImagePath As Variant
Dim ImageName As String
Dim S1 As String
Dim i As Integer
Dim intItemsInList As Integer
Dim intCounter As Integer
Dim num1, num2, num3, num4, num5, num6, num7, num8
Public Sub Reset()
txtInvoiceID.Value = AutoNumberReturn("tblInvoice", "InvoiceID")
txtInvoiceNo.Value = "AplNr-" & AutoNumberReturn("tblInvoice", "InvoiceID")
txtInvoiceDate.Value = Date
cboSales.Value = ""
txtSalesManID.Value = ""
txtsalesmanName.Value = ""
txtCustomerID.Value = Null
txtRemarks.Value = Null
cboCustomerName.Value = ""
cboProduct.Value = ""
txtGroup.Value = ""
txtContactNo.Value = Null
listBox1.RowSource = ""
txtCrop.Value = ""
txtGreenhouseNr.Value = ""
txtPropagator.Value = ""
btnSave.Enabled = True
btnNew.Enabled = True
btnDelete.Enabled = False
btnUpdate.Enabled = False
btnPrint.Enabled = False
Clear
End Sub
Public Sub Clear()
txtProductID.Value = ""
txtPID.Value = ""
cboProduct.Value = ""
txtPropagator.Value = ""
txtQTY.Value = "0"
txtGreenhouseNr.Value = ""
'txtCrop.Value = ""
End Sub
Private Sub btnAdd_Click()
Dim D As String
Dim p1, p2, p3, p4, p5, p6
p1 = "'" & txtPID.Value & "'"
p2 = "'" & txtProductID.Value & "'"
p3 = "'" & txtProductName.Value & "'"
p4 = "'" & txtPropagator.Value & "'"
p5 = "'" & txtGreenhouseNr.Value & "'"
p6 = "'" & txtQTY.Value & "'"
If IsNull(txtProductID.Value) Then
MsgBox "Please Retrieve Product ID", vbInformation, "Information"
txtProductID.SetFocus
Exit Sub
End If
If IsNull(txtQTY.Value) Then
MsgBox "Please Enter Quantity", vbInformation, "Information"
txtQTY.SetFocus
Exit Sub
End If
If IsNull(txtQTY.Value = 0) Then
MsgBox "Quantity can not be zero", vbInformation, "Information"
txtQTY.SetFocus
Exit Sub
End If
D = p1 & ";" & p2 & ";" & p3 & ";" & p4 & ";" & p5 & ";" & p6 & ";"
listBox1.AddItem D
' Call clean(Me)
txtQTY.SetFocus
Clear
End Sub
Private Sub btnNew_Click()
Reset
End Sub
Private Sub btnPurchaseList_Click()
DoCmd.OpenForm "frmSalesList", acNormal
End Sub
Private Sub btnRemove_Click()
Dim ii As Integer
With listBox1
For ii = 0 To .ListCount - 1
If .Selected(ii) = True Then
.RemoveItem ii
End If
Next
If .ListCount <= 0 Then
' btnRemove.Enabled = False
btnAdd.Enabled = True
End If
End With
End Sub
Private Sub btnSave_Click()
If txtSalesManID = "" Then
MsgBox "Please Retrieve Sales Man ID.", vbInformation, "Choose SalesMan"
txtSalesManID.SetFocus
Exit Sub
End If
DoCmd.runSQL "INSERT INTO tblinvoice(InvoiceID,InvoiceNo,InvoiceDate,CustomerID,SalesManID,Remarks) VALUES('" & txtInvoiceID & "','" & txtInvoiceNo & "','" & txtInvoiceDate & "','" & txtCustomerID & "','" & txtSalesManID & "','" & txtRemarks & "')"
For i = 0 To listBox1.ListCount - 1 Step 1
Db.Execute "INSERT INTO InvoiceJoin(InvoiceID,ProductID,Propagator,GreenhouseNr,Qty) VALUES('" & txtInvoiceID & "', '" & listBox1.Column(0, i) & "' , '" & listBox1.Column(3, i) & "' , '" & listBox1.Column(4, i) & "' , '" & listBox1.Column(5, i) & "')"
Next
MsgBox "Successfully done", vbInformation, "Sales"
btnPrint.Enabled = True