Question Reading a value from database into an integer

mauni

Registered User.
Local time
Today, 08:24
Joined
Feb 10, 2009
Messages
23
Hi

I need to read the latest value of invoiceid from a table, into an integer called A1.


Table INVOICE:

invoiceid AS integer
invoicename AS string


I stored a query for the search. I can read/modify the query with this code:

Dim db As dao.Database
Dim qdef As dao.QueryDef
Dim strSQL As String
Set db = CurrentDb
strSQL = "SELECT invoiceid from invoice;"
Set qdef = db.QueryDefs("search_latest_invoiceid")
qdef.sql = strSQL
qdef.Close
Set qdef = Nothing
Set db = Nothing
DoCmd.OpenQuery "search_latest_invoiceid", acViewNormal


This query returns all invoiceid values.

How can i read the latest invoiceid into an integer called A1 ?
 
Thanks DennisK, that code works. I was able to output the correct value also with:

SELECT TOP 1 invoiceid FROM invoice ORDER BY invoiceid DESC;


But how can i transfer the value into an integer? Now it only opens the result into an openquery window.
 
You seem to be going about this the wrong way. Try.

Dim A1 As Integer

A1 = DLast("InvoiceID","Invoice")
 
totally untested aircode, but maybe it will work:

Code:
Dim myInvioceID As String
Dim myInt As Integer

myInvoiceID = "SELECT LAST(InvoiceID) FROM Invoice"
myInt = myInvoiceID
 
ah, DCrake, always one step ahead ;) and with more elegant code LOL
 
make it a long, not an integer possibly

an integer stops at 32000 odd, which may cause issues
 
Mauni,

I think we all knew that it was just a case of simplifying the code. And not only that but using the correct code for the job.

David
 
make it a long, not an integer possibly

an integer stops at 32000 odd, which may cause issues


ahhh! good point :) never even thought about that for an SQL statement (only really ever considered it in table design)

thanks gemma-the-husky :)
 
i very rarely declare integers - pretty well always longs just in case
 

Users who are viewing this thread

Back
Top Bottom