RageMatrix
Programmer
Hey all,
Which method is the best way to add a record to an Access database? The first way I used was to use data bound controls on the form and then use something like this to save the record: -
Private Sub cmdCustEntrySaveRecord_Click()
On Error GoTo Err_cmdCustEntrySaveRecord_Click
' Save new record.
Answer = MsgBox("Save current record before starting a new one?", vbYesNo, "Save Before New"
Select Case Answer
Case vbYes
If IsNull([CustomerID]) Then
Answer = MsgBox("This record is empty. It will not be saved", vbOKOnly, "Database"
End If
Case vbNo
Me.Undo
End Select
Me.Refresh
Me.Recordset.AddNew
Exit_cmdCustEntrySaveRecord_Click:
Exit Sub
Err_cmdCustEntrySaveRecord_Click:
MsgBox Err.Description
Resume Exit_cmdCustEntrySaveRecord_Click
End Sub
The other way I've used is just for the "Save" button to pass all the values to be added to a function like so: -
Function AddNewLoan(txtCustID As String, txtVidID As String, dtmDateLoaned As Date, dtmDateDueBack As Date) As Boolean
' Add new child loan record to the parent customer.
On Error GoTo ErrHandler
Dim rsRecordset As DAO.Recordset
Dim objDatabase As DAO.Database
If IsNull(txtCustID) Or IsNull(txtVidID) Or IsNull(dtmDateLoaned) Or IsNull(dtmDateDueBack) Then
AddNewLoan = False
Else
Set objDatabase = CurrentDb
Set rsRecordset = objDatabase.OpenRecordset("SELECT * FROM tblLoans WHERE CustomerID = " & txtCustID)
With rsRecordset
.AddNew
.Fields("CustomerID"
= Trim(txtCustID)
.Fields("VideoID"
= Trim(txtVidID)
.Fields("DateLoaned"
= dtmDateLoaned
.Fields("DateDueBack"
= dtmDateDueBack
.Fields("Overdue"
= False
.Update
.Close
End With
Set rsRecordset = Nothing
AddNewLoan = True
End If
Exit Function
' Error detection.
ErrHandler:
AddNewLoan = False
Exit Function
End Function
What is the "accepted" way to add records to an Access database?
Thanks for any enlightenment.
Jon.
Which method is the best way to add a record to an Access database? The first way I used was to use data bound controls on the form and then use something like this to save the record: -
Private Sub cmdCustEntrySaveRecord_Click()
On Error GoTo Err_cmdCustEntrySaveRecord_Click
' Save new record.
Answer = MsgBox("Save current record before starting a new one?", vbYesNo, "Save Before New"
Select Case Answer
Case vbYes
If IsNull([CustomerID]) Then
Answer = MsgBox("This record is empty. It will not be saved", vbOKOnly, "Database"
End If
Case vbNo
Me.Undo
End Select
Me.Refresh
Me.Recordset.AddNew
Exit_cmdCustEntrySaveRecord_Click:
Exit Sub
Err_cmdCustEntrySaveRecord_Click:
MsgBox Err.Description
Resume Exit_cmdCustEntrySaveRecord_Click
End Sub
The other way I've used is just for the "Save" button to pass all the values to be added to a function like so: -
Function AddNewLoan(txtCustID As String, txtVidID As String, dtmDateLoaned As Date, dtmDateDueBack As Date) As Boolean
' Add new child loan record to the parent customer.
On Error GoTo ErrHandler
Dim rsRecordset As DAO.Recordset
Dim objDatabase As DAO.Database
If IsNull(txtCustID) Or IsNull(txtVidID) Or IsNull(dtmDateLoaned) Or IsNull(dtmDateDueBack) Then
AddNewLoan = False
Else
Set objDatabase = CurrentDb
Set rsRecordset = objDatabase.OpenRecordset("SELECT * FROM tblLoans WHERE CustomerID = " & txtCustID)
With rsRecordset
.AddNew
.Fields("CustomerID"
.Fields("VideoID"
.Fields("DateLoaned"
.Fields("DateDueBack"
.Fields("Overdue"
.Update
.Close
End With
Set rsRecordset = Nothing
AddNewLoan = True
End If
Exit Function
' Error detection.
ErrHandler:
AddNewLoan = False
Exit Function
End Function
What is the "accepted" way to add records to an Access database?
Thanks for any enlightenment.
Jon.