Ally
Registered User.
- Local time
- Today, 23:57
- Joined
- Sep 18, 2001
- Messages
- 617
I've got some code on a form, that posts fields to a subform, (therefore a table).
There is code (in a module) to trap whether any fields have been left null, but if there are, I'm getting a data conversion error for some of the numeric / currency fields. So rather than declaring them as currency and integer, I changed it to variant. Then tried to change it back as it posts to the table, but along the line I'm still getting an error.
Is there a simple way of doing this - ie, declaring it as CCur / CInt somehow? I had a go, but couldn't work it out.
Sorry - but my coding skills are appalling and get very confused! (And I don't take the credit for the good bits as Col wrote a lot of it and I just amended to suit my form)!
Here's the code. If you need any more info, please let me know. Errors occur in the lines emboldened.
Module Code:
There is code (in a module) to trap whether any fields have been left null, but if there are, I'm getting a data conversion error for some of the numeric / currency fields. So rather than declaring them as currency and integer, I changed it to variant. Then tried to change it back as it posts to the table, but along the line I'm still getting an error.
Is there a simple way of doing this - ie, declaring it as CCur / CInt somehow? I had a go, but couldn't work it out.
Sorry - but my coding skills are appalling and get very confused! (And I don't take the credit for the good bits as Col wrote a lot of it and I just amended to suit my form)!
Here's the code. If you need any more info, please let me know. Errors occur in the lines emboldened.
Code:
Private Sub cmdSaveData_Click()
Dim bx1 As Variant, x As Integer, y As String, z As Variant, r As Recordset
Dim db As Database, a As String, b As Integer, c As Variant, d As Variant, ctl As Control, frm As Form
Dim e As String, f As Variant, g As Integer
Set db = CurrentDb
Set r = db.OpenRecordset("tblIssues")
Call CheckFields
Set frm = Screen.ActiveForm
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox And ctl.Tag = "xyz" And IsNull(ctl) Then
DoCmd.GoToControl ctl.ControlName
ctl.Tag = "req"
msgbox ctl.Tag
Exit Sub
End If
Next ctl
x = Me.OrderID.Value
y = Me.txtItemSelect.Value
[b]z = Me.txtQuantity.Value
c = Me.txtCost.Value[/b]
d = Me.txtNumberSent.Value
e = Me.txtBatchNo.Value
f = Me.txtExpDate.Value
If Me.txtNumberSent > Me.txtQuantity Then
msgbox "Number sent is greater than number ordered! Please amend.", vbOKOnly, "Quantity Error"
DoCmd.GoToControl "txtQuantity"
Exit Sub
End If
If Me.txtQuantity > Me.txtNumberSent Then
Me.PartOrder.Caption = "Part Order"
Me.PartOrderTick = -1
Else
Me.PartOrder.Caption = ""
Me.PartOrderTick = 0
End If
a = Me.PartOrderTick.Value
'posts new issue into issues table
r.AddNew
r.Fields("OrderID") = x
r.Fields("ProductID") = y
[b]r.Fields("QuantityOrdered") = CInt(z)
r.Fields("UnitPrice") = CCur(c)[/b]
r.Fields("QuantityDelivered") = d
r.Fields("BatchNumber") = e
r.Fields("ExpiryDate") = f
r.Fields("PartOrder") = a
r.Update
DoCmd.Requery "Issues Subform"
bx1 = msgbox("Do you want to enter any more items" _
& " for this order?", vbYesNo + vbQuestion, "Note")
If bx1 = vbYes Then
Me.txtItemSelect.Value = ""
Me.txtQuantity.Value = ""
Me.txtCost.Value = ""
Me.txtNumberSent.Value = ""
Me.txtBatchNo.Value = ""
Me.txtExpDate.Value = ""
DoCmd.OpenForm ("frmDrugSearch")
Else
Me.txtItemSelect.Value = ""
Me.txtQuantity.Value = ""
Me.txtCost.Value = ""
Me.txtNumberSent.Value = ""
Me.txtBatchNo.Value = ""
Me.txtExpDate.Value = ""
Exit Sub
End If
lexit:
Exit Sub
lerror:
msgbox Err.Description
Resume lexit
End Sub
Module Code:
Code:
Public Function CheckFields()
Dim ctl As Control, frm As Form
Set frm = Screen.ActiveForm
For Each ctl In frm.Controls
If ctl.ControlType = acTextBox And ctl.Tag = "req" And IsNull(ctl) Then
msgbox ctl.ControlName & " has no value"
' DoCmd.GoToControl ctl
ctl.Tag = "xyz"
msgbox ctl.Tag
Exit Function
Else
GoTo 10
End If
10: Next ctl
End Function