I am trying to shift from an excel database to an Access database to allow multi-user inputs. I have a userform, which asks for user inputs, and it generates a file number for them by incrementing the last file number in the database. This is the working vba code for excel as database.
Sub Submit()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.AutomationSecurity = msoAutomationSecurityLow
If frmForm.txtDosage.Value = "" Or frmForm.txtProject.Value = "" Or frmForm.txtTime.Value = "" Then
MsgBox ("Complete All fields marked with (*) to proceed")
Else
Dim nwb As Workbook
Set nwb = Workbooks.Open("C:\Users\CHAMARA2.APNET\Automatic File Number Creation\AFNC Database.xlsm")
Dim emptyRow As Long
Dim lastinvoice As String
Dim newfile As String
emptyRow = WorksheetFunction.CountA(nwb.Sheets("Sheet1").Range("A:A")) + 1
lastinvoice = nwb.Sheets("Sheet1").Cells(emptyRow - 1, 7)
With nwb.Sheets("Sheet1")
.Cells(emptyRow, 1) = emptyRow - 1
.Cells(emptyRow, 2) = frmForm.txtProject.Value
.Cells(emptyRow, 3) = frmForm.txtDosage.Value
.Cells(emptyRow, 5) = frmForm.txtTime.Value
.Cells(emptyRow, 6) = Application.UserName
.Cells(emptyRow, 4) = frmForm.cmbPurpose.Value
.Cells(emptyRow, 7) = Left(lastinvoice, 4) & "-" & Format(Int(Right(lastinvoice, 3)) + 1, "000")
.Cells(emptyRow, 8) = Date
newfile = .Cells(emptyRow, 7).Value
End With
End If
MsgBox ("Your generated file number is " & newfile)
nwb.SaveAs Filename:="C:\Users\CHAMARA2.APNET\Automatic File Number Creation\AFNC Database.xlsm"
nwb.Close
End Sub
And this is the code for access:
Sub Submit2()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Application.AutomationSecurity = msoAutomationSecurityLow
If frmForm.txtDosage.Value = "" Or frmForm.txtProject.Value = "" Or frmForm.txtTime.Value = "" Then
MsgBox ("Complete All fields marked with (*) to proceed")
Else
Dim cnn As New ADODB.Connection 'dim the ADO collection class
Dim rst As New ADODB.Recordset 'dim the ADO recordset class
Dim dbPath As String
dbPath = "C:\Users\CHAMARA2.APNET\Downloads\TestDB.accdb"
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath
Set rst = New ADODB.Recordset 'assign memory to the recordset
rst.Open Source:="FileNumbers", ActiveConnection:=cnn, _
CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
'Dim emptyRow As Long
'Dim lastinvoice As String
'Dim newfile As String
'emptyRow = WorksheetFunction.CountA(nwb.Sheets("Sheet1").Range("A:A")) + 1
'lastinvoice = nwb.Sheets("Sheet1").Cells(emptyRow - 1, 7)
With rst
.AddNew
.Fields("Project").Value = frmForm.txtProject.Value
.Fields("Dose").Value = frmForm.txtDosage.Value
.Fields("Time Point").Value = frmForm.txtTime.Value
.Fields("Submitted By").Value = Application.UserName
.Fields("Purpose").Value = frmForm.cmbPurpose.Value
.Fields("File Number").Value = Left(lastinvoice, 4) & "-" & Format(Int(Right(lastinvoice, 3)) + 1, "000")
.Fields("Date Created").Value = Date
.Update
'newfile = .Cells(emptyRow, 7).Value
End With
End If
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
MsgBox ("Your generated file number is " & newfile)
End Sub
How can I achieve something similar for the File Number field with the access code? And then getting the generated file number to the newfile variable as well, so that I can show it as a MsgBox.
This is the sequence of the file numbers: INHY-101, INHY-102, INHY-103 and so on
Please help