Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert String to Number 1

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
US
I have a field that is essentially a number, but the data type is a string. I need to:
1. convert the value to a number
2. find the max value
3. add 1 to the value and set it equal to a variable
4. convert it back to string and store it in a variable

here is what i have so far:

SqlStr3 = "select max(CInt(speedbidnum)) from bidders" ???
Set DB = CurrentDb
Set qdfTemp = DB.CreateQueryDef("", SqlStr)
Set ChkRst = qdfTemp.OpenRecordset(dbOpenDynaset, dbSeeChanges)

BidNum=ChkRst!speedbidnum + 1
Convert to String Function(BidNum) ???

ChkRst.Close
Set ChkRst = Nothing

'Apend leading zeros so the length is 6 characters
do until len(bidnum) = 6
bidnum = 0 & bidnum
loop

I have been experimenting with access queries to get the max value as a number, but I get an error:
Invalid Use of Null Thank you for your assistance

Jason Meckley
Database Analyst
WITF
 
This should do what you need.

Dim BidNum as String
BidNum = Format(DMax("Val(speedbidnumber)","bidders")+1,"000000")

Paul
 
Thanx for your help Paul. The exact formula gave me syntax errors, but pointed me in the right direction. The following code was able to accomplish what I was looking for.
SqlStr = "SELECT Format(Max(Val([speedBidNumber]))+1,""000000"")as Num FROM bidder WHERE (((bidder.speedBidNumber) Is Not Null));"
Set DB = CurrentDb
Set qdfTemp = DB.CreateQueryDef("", SqlStr)
Set ChkRst = qdfTemp.OpenRecordset(dbOpenDynaset, dbSeeChanges)

BidNum = ChkRst!num

ChkRst.Close
Set ChkRst = Nothing

SqlStr = "UPDATE bidder SET bidder.speedBidNumber = '" & BidNum & "' WHERE bidder.BidderCode='" & Me.BidderCode.Value & "';"
DoCmd.SetWarnings False
DoCmd.RunSQL SqlStr
DoCmd.SetWarnings True
Forms!Results_frm.Requery Thank you for your assistance

Jason Meckley
Database Analyst
WITF
 
Yes, putting it inside a Select string would change the syntax some. Glad it helped.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top