A
adamskiii
I am starting to learn to use form is access and have created an unbound form
because I believe you have more control over table records. My question is,
how do I validate fields on my form first and then once all validation are
correct and no errors appear it saves the record? This is currently my code:
Option Explicit
Option Compare Database
Dim connection As New ADODB.connection
Dim part As New ADODB.Recordset
Private Sub Form_Load()
Set connection = CurrentProject.connection
part.Open "SELECT * FROM part ORDER BY partID", connection, _
adOpenDynamic, adLockOptimistic
populateForm
BrowseMode (True)
End Sub
Private Sub populateForm()
If part.EOF Then
part.MoveLast
ElseIf part.BOF Then
part.MoveFirst
End If
Me.txtPartId = part.Fields("partId")
Me.txtCost = part.Fields("cost")
Me.txtDescription = part.Fields("description")
Me.txtOnHand = part.Fields("onHand")
Me.txtOnOrder = part.Fields("onOrder")
Me.txtListPrice = part.Fields("listPrice")
Me.cmboVendor = part.Fields("vendorID")
End Sub
Private Sub BrowseMode(value As Boolean)
Me.txtCost.Locked = value
Me.txtDescription.Locked = value
Me.txtListPrice.Locked = value
Me.txtOnHand.Locked = value
Me.txtOnOrder.Locked = value
Me.txtPartId.Locked = value
Me.cmboVendor.Locked = value
Me.cmdFirst.Enabled = value
Me.cmdLast.Enabled = value
Me.cmdNext.Enabled = value
Me.cmdPrevious.Enabled = value
If value Then
Me.cmdNext.SetFocus
Else
Me.txtPartId.SetFocus
End If
Me.btnAdd.Enabled = value
Me.btnCancel.Enabled = Not value
Me.btnEdit.Enabled = value
Me.cmdExit.Enabled = value
Me.btnSave.Enabled = Not value
End Sub
Private Sub cmdFirst_Click()
part.MoveFirst
populateForm
End Sub
Private Sub cmdLast_Click()
part.MoveLast
populateForm
End Sub
Private Sub cmdNext_Click()
part.MoveNext
populateForm
End Sub
Private Sub cmdPrevious_Click()
part.MovePrevious
populateForm
End Sub
Private Sub btnCancel_Click()
BrowseMode (True)
part.CancelUpdate
populateForm
End Sub
Private Sub btnEdit_Click()
BrowseMode (False)
End Sub
Private Sub btnAdd_Click()
BrowseMode (False)
part.AddNew
populateForm
End Sub
Private Sub btnSave_Click()
part.Fields("partId") = Me.txtPartId.value
part.Fields("cost") = Me.txtCost.value
part.Fields("description") = Me.txtDescription.value
part.Fields("onHand") = Me.txtOnHand.value
part.Fields("onOrder") = Me.txtOnOrder.value
part.Fields("listPrice") = Me.txtListPrice.value
part.Update
BrowseMode (True)
End Sub
Private Sub cmdExit_Click()
DoCmd.Close
End Sub
In the btnSave_Click() I want it to first check a function called EDIT for
all validations. Once all have passed and no errors appear then the form data
will be saved. How can I do this? I tried adding the EDIT function:
Private Function edit(editMe As Boolean)
If (Me.txtCost >= Me.txtListPrice) Then
MsgBox "ERROR"
End If
End Function
And then in my btnSave_Click() I added:
Private Sub btnSave_Click()
if edit = True Then
part.Fields("partId") = Me.txtPartId.value
part.Fields("cost") = Me.txtCost.value
part.Fields("description") = Me.txtDescription.value
part.Fields("onHand") = Me.txtOnHand.value
part.Fields("onOrder") = Me.txtOnOrder.value
part.Fields("listPrice") = Me.txtListPrice.value
part.Update
BrowseMode (True)
End If
End Sub
But this is not working. Any help?
Thanks
because I believe you have more control over table records. My question is,
how do I validate fields on my form first and then once all validation are
correct and no errors appear it saves the record? This is currently my code:
Option Explicit
Option Compare Database
Dim connection As New ADODB.connection
Dim part As New ADODB.Recordset
Private Sub Form_Load()
Set connection = CurrentProject.connection
part.Open "SELECT * FROM part ORDER BY partID", connection, _
adOpenDynamic, adLockOptimistic
populateForm
BrowseMode (True)
End Sub
Private Sub populateForm()
If part.EOF Then
part.MoveLast
ElseIf part.BOF Then
part.MoveFirst
End If
Me.txtPartId = part.Fields("partId")
Me.txtCost = part.Fields("cost")
Me.txtDescription = part.Fields("description")
Me.txtOnHand = part.Fields("onHand")
Me.txtOnOrder = part.Fields("onOrder")
Me.txtListPrice = part.Fields("listPrice")
Me.cmboVendor = part.Fields("vendorID")
End Sub
Private Sub BrowseMode(value As Boolean)
Me.txtCost.Locked = value
Me.txtDescription.Locked = value
Me.txtListPrice.Locked = value
Me.txtOnHand.Locked = value
Me.txtOnOrder.Locked = value
Me.txtPartId.Locked = value
Me.cmboVendor.Locked = value
Me.cmdFirst.Enabled = value
Me.cmdLast.Enabled = value
Me.cmdNext.Enabled = value
Me.cmdPrevious.Enabled = value
If value Then
Me.cmdNext.SetFocus
Else
Me.txtPartId.SetFocus
End If
Me.btnAdd.Enabled = value
Me.btnCancel.Enabled = Not value
Me.btnEdit.Enabled = value
Me.cmdExit.Enabled = value
Me.btnSave.Enabled = Not value
End Sub
Private Sub cmdFirst_Click()
part.MoveFirst
populateForm
End Sub
Private Sub cmdLast_Click()
part.MoveLast
populateForm
End Sub
Private Sub cmdNext_Click()
part.MoveNext
populateForm
End Sub
Private Sub cmdPrevious_Click()
part.MovePrevious
populateForm
End Sub
Private Sub btnCancel_Click()
BrowseMode (True)
part.CancelUpdate
populateForm
End Sub
Private Sub btnEdit_Click()
BrowseMode (False)
End Sub
Private Sub btnAdd_Click()
BrowseMode (False)
part.AddNew
populateForm
End Sub
Private Sub btnSave_Click()
part.Fields("partId") = Me.txtPartId.value
part.Fields("cost") = Me.txtCost.value
part.Fields("description") = Me.txtDescription.value
part.Fields("onHand") = Me.txtOnHand.value
part.Fields("onOrder") = Me.txtOnOrder.value
part.Fields("listPrice") = Me.txtListPrice.value
part.Update
BrowseMode (True)
End Sub
Private Sub cmdExit_Click()
DoCmd.Close
End Sub
In the btnSave_Click() I want it to first check a function called EDIT for
all validations. Once all have passed and no errors appear then the form data
will be saved. How can I do this? I tried adding the EDIT function:
Private Function edit(editMe As Boolean)
If (Me.txtCost >= Me.txtListPrice) Then
MsgBox "ERROR"
End If
End Function
And then in my btnSave_Click() I added:
Private Sub btnSave_Click()
if edit = True Then
part.Fields("partId") = Me.txtPartId.value
part.Fields("cost") = Me.txtCost.value
part.Fields("description") = Me.txtDescription.value
part.Fields("onHand") = Me.txtOnHand.value
part.Fields("onOrder") = Me.txtOnOrder.value
part.Fields("listPrice") = Me.txtListPrice.value
part.Update
BrowseMode (True)
End If
End Sub
But this is not working. Any help?
Thanks