Get the next voucher number from access column

mopey12345

Board Regular
Joined
Nov 26, 2020
Messages
76
Office Version
  1. 2010
Platform
  1. Windows
Hi,
Any help appreciated. I have an excel spreadsheet and need to extract the next voucher number from an access table Customers, column called Voucher. Thanks Phil
Sub getmaxvoucher_number()
Dim cn As Object, rs As Object
Dim intColIndex As Integer
Dim DBFullName As String
Dim TargetRange As Range

DBFullName = "X:\MyDocuments\Advance\Advance.mdb"

On Error GoTo End_

Application.ScreenUpdating = False
Set TargetRange = Sheets("Sheet1").Range("A1")

Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBFullName & ";"

Set rs = CreateObject("ADODB.Recordset")
rs.Open "SELECT * FROM Customers WHERE Dmax[Voucher],cn, , , adCmdText"

' Write the field names
For intColIndex = 0 To rs.Fields.Count - 1
TargetRange.Offset(1, intColIndex).Value = rs.Fields(intColIndex).Name
Next

' Write recordset
TargetRange.Offset(1, 0).CopyFromRecordset rs

LetsContinue:
Application.ScreenUpdating = True
On Error Resume Next
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
On Error GoTo 0
Exit Sub
End_:
MsgBox "Error Description :" & Err.Description & vbCrLf & _
"Error at line :" & Erl & vbCrLf & _
"Error Number :" & Err.Number
Resume LetsContinue
End If
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
No code tags? :(
Generally it would be Dmax() + 1, but how do you update the DB to reflect that this new Voucher number has been used?, so if run again you get the next free voucher number?
See the syntax for Dmax() Application.DMax method (Access)

I would suspect you would use Select Max(Voucher) and then add 1 to it. Still have the update issue?
 
Upvote 0
No code tags? :(
Generally it would be Dmax() + 1, but how do you update the DB to reflect that this new Voucher number has been used?, so if run again you get the next free voucher number?
See the syntax for Dmax() Application.DMax method (Access)

I would suspect you would use Select Max(Voucher) and then add 1 to it. Still have the update issue?
Thanks for responding. The excel front end gets the latest number (+1 )from the access table, does the process and then writes back to the Access table a new record with this lastest number. Hope that makes sense. thanks Phil
 
Upvote 0
Im not sure how to do this but I think I need to open the Access database Customers table from Excel front end (vba) and run a query on the voucher column to get the latest voucher number. Process the case with voucher number +1 and then write back a new record to the Customer table.
 
Upvote 0
Yes, that is what I was saying? :unsure: You seem to be saying the same thing?
Not sure what all the fields are for though?

I would Select Voucher from from Customer WHERE as you have it now.
Add 1 to Voucher giving lngVoucher
Then use rs.Edit, assign lngVoucher to Voucher, and then rs.Update

Code:
lngVoucher = rs!Voucher + 1
rs.edit
rs!Voucher = lngVoucher
rs.Update
and use the lngVoucher for your next number.
If you need several in a sequence, do the math in Excel and update at the end.

Possible conflict in multi user scenario in that case though?
 
Upvote 0
Yes, that is what I was saying? :unsure: You seem to be saying the same thing?
Not sure what all the fields are for though?

I would Select Voucher from from Customer WHERE as you have it now.
Add 1 to Voucher giving lngVoucher
Then use rs.Edit, assign lngVoucher to Voucher, and then rs.Update

Code:
lngVoucher = rs!Voucher + 1
rs.edit
rs!Voucher = lngVoucher
rs.Update
and use the lngVoucher for your next number.
If you need several in a sequence, do the math in Excel and update at the end.

Possible conflict in multi user scenario in that case though?
Thanks for replying. Probably a bit out of my depth here, but got this code using the query wizard SQl view,
SELECT Max(Customers.Voucher) AS MaxOfVoucher
FROM Customers
ORDER BY Max(Customers.Voucher);
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top