copy data to table

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Transfer data 14
Send data to table 11
Save Deleted Record 16
OpenRecordSet Error 3
User Defining Problem 2
New ID 1
Capture Date deleted 5
record changed values 1

Top