D
Dan @BCBS
I need to copy data to a table. The user enters data on a form and clicks a
command button. It should get a new ID and enter two values. The GetNewID
works off a public function (See second code below).
Here are both codes I have created.
1. Command button Code:
Private Sub cmdAddProv_Click()
Dim lCriteria As String
Dim lPROVNUM As String
DoCmd.SetWarnings False
Dim lID As Long
lID = GetNewID("tblProviders")
lCriteria = "INSERT INTO tblProviders ( PROVNO, ZipCD ) "
lCriteria = lCriteria & "tblProviders.PROVNO,
tblProviders.ZipCD, "
lCriteria = lCriteria & "WHERE (((tblProviders.PROVNO)=" & """"
& lPROVNUM & """" & "));"
DoCmd.RunSQL lCriteria
DoCmd.SetWarnings True
DoCmd.GoToRecord , , acNewRec
Exit Sub
End Sub
2. GetNewID public function:
Public Function GetNewID(tblName As String) As Long
Dim db As Database
Dim RS As Recordset
Set db = CurrentDb
Set RS = db.OpenRecordset(tblName)
If RS.RecordCount > 0 Then
RS.MoveLast
GetNewID = RS.Fields(0) + 1
Else
GetNewID = 0
End If
End Function
command button. It should get a new ID and enter two values. The GetNewID
works off a public function (See second code below).
Here are both codes I have created.
1. Command button Code:
Private Sub cmdAddProv_Click()
Dim lCriteria As String
Dim lPROVNUM As String
DoCmd.SetWarnings False
Dim lID As Long
lID = GetNewID("tblProviders")
lCriteria = "INSERT INTO tblProviders ( PROVNO, ZipCD ) "
lCriteria = lCriteria & "tblProviders.PROVNO,
tblProviders.ZipCD, "
lCriteria = lCriteria & "WHERE (((tblProviders.PROVNO)=" & """"
& lPROVNUM & """" & "));"
DoCmd.RunSQL lCriteria
DoCmd.SetWarnings True
DoCmd.GoToRecord , , acNewRec
Exit Sub
End Sub
2. GetNewID public function:
Public Function GetNewID(tblName As String) As Long
Dim db As Database
Dim RS As Recordset
Set db = CurrentDb
Set RS = db.OpenRecordset(tblName)
If RS.RecordCount > 0 Then
RS.MoveLast
GetNewID = RS.Fields(0) + 1
Else
GetNewID = 0
End If
End Function