Hello all,
I'm using DAO to insert data (entered in unbound controls on a unbound form) into a table.
Before running the code to insert the the data I must check that all fields on the form are not empty.
I don't want to Set the Required property of the fields to Yes since the error message produced isn't user friendly.
I believe I have to check that no field is empty either before running the appending code or on the form before update.
So what I'm asking is what code is necessary to check that all fields are filled.
I tried this in both the form before update and and before the appending code, but it didn't stop the empty data.
This is the code I use for appending the data to the tables.
I'm using DAO to insert data (entered in unbound controls on a unbound form) into a table.
Before running the code to insert the the data I must check that all fields on the form are not empty.
I don't want to Set the Required property of the fields to Yes since the error message produced isn't user friendly.
I believe I have to check that no field is empty either before running the appending code or on the form before update.
So what I'm asking is what code is necessary to check that all fields are filled.
I tried this in both the form before update and and before the appending code, but it didn't stop the empty data.
Code:
Dim ctl As Control
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.Value = "" Then
MsgBox "Missing required information." & vbCrLf & "Please fill in all missing information and try again.", vbCritical, "Can't Save"
End If
Case Else
End Select
Next ctl
This is the code I use for appending the data to the tables.
Code:
Private Sub cmdUpdate_Click()
On Error GoTo catch
Dim ctl As Control
Dim db As DAO.Database
Dim rs1 As Recordset
Dim rs2 As Recordset
Set db = CurrentDb()
Set rs1 = db.OpenRecordset("tblContacts", dbOpenDynaset)
Set rs2 = db.OpenRecordset("tblRequests", dbOpenDynaset)
' Insert info from form to tables
With rs1
.AddNew
![Surname] = [txtSurname]
![FirstName] = [txtFirstname]
![Phone] = [txtPhone]
![Fax] = [txtFax]
![Email] = [txtEmail]
.Update
End With
With rs2
.AddNew
![RequestedCity] = [cboCity]
![RequestedNeighborhood] = [cboNeighborhood]
![RequestedRooms] = [cboRooms]
![RequestedPriceTop] = [txtPrice]
![Comments] = [txtComments]
.Update
End With
' Clear all fields
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl
catch:
MsgBox "Error#: " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub