generating an auto number

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2005
Posts: 2
Reputation: nawaray is an unknown quantity at this point 
Solved Threads: 0
nawaray nawaray is offline Offline
Newbie Poster

generating an auto number

 
0
  #1
Apr 20th, 2005
:ehh: helloo...

i have this renting application in which the user enters his name, phone , amount, date , payment to rent a flat ,,

i have a table called rent which is related to this its fields are :

name, telephone,amount, date, paid and there is one more field called serial which is the primary key, i want every user to have a different serial number ( which will be generated automatically every time i add a user)

for example:

when user one will be added he will have serial number 1

when user two is added he will have serial number 2

user 10 serial 10 and so on ,, i tried doing many things but it doesnt work ,

this is my code for the add button:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdSave_Click()
  2. adoBooks.Recordset.Fields("Name") = txtName.Text
  3. adoBooks.Recordset.Fields("telephone") = txtPhone.Text
  4. adoBooks.Recordset.Fields("amount") = txtAmount.Text
  5. adoBooks.Recordset.Fields("Date") = txtDate.Text
  6. adoBooks.Recordset.Fields("paid") = Check1.Value
  7. adoBooks.Recordset.Fields("serial") = txtSerial.Text
  8.  
  9. adoBooks.Recordset.Fields("serial") = serial
  10. adoBooks.Recordset.Update


and this is when the form is loaded:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. serial = adoBooks.Recordset.Fields("serial")
  2. serial = serial + 1

where serial is an integer

i tried doing this: ( by adding a function)
Public Function getUniqID(tblname As String, fldname As String) As Long
    Dim tmpRs As ADODB.Recordset
    Dim MaxID As Long
Set tmpRs = New ADODB.Recordset
 tmpRs.CursorLocation = adUseClient
   tmpRs.Open "select max(" & fldname & ") as maxID from " & tblname ', Cnn, adOpenDynamic, adLockOptimistic
   If IsNull(tmpRs("maxID")) = True Then 
MaxID = 0 
Else MaxID = tmpRs("maxID")
  If MaxID = 0 Then
   intSerialNo = 1  
 Else
    intSerialNo = MaxID + 1
    End If
    tmpRs.Close
End Function


Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2.  
  3. Call getUniqID("rent", "serial")
  4.  
  5. End Sub

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdSave_Click()
  2.  
  3. Call getUniqID("rent", "serial")
  4.  
  5. adoBooks.Recordset.Fields("Name") = txtName.Text
  6. adoBooks.Recordset.Fields("telephone") = txtPhone.Text
  7. adoBooks.Recordset.Fields("amount") = txtAmount.Text
  8. adoBooks.Recordset.Fields("Date") = txtDate.Text
  9. adoBooks.Recordset.Fields("paid") = Check1.Value
  10.  
  11. adoBooks.Recordset.Fields("serial") = intSerialNo
  12. adoBooks.Recordset.Update
  13.  
  14. End Sub

but i keep on getting an error in the line colored red
any help please?

nawaray
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: generating an auto number

 
0
  #2
Apr 20th, 2005
Something to consider, is setting the database reference to nothing. This should close and unload the adobooks object from memory, and allow you to re-create, and re-open it. I might be mistaken, but it looks like it's mad that you are trying to open a database that is already open.....
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 4
Reputation: Mike Vance is an unknown quantity at this point 
Solved Threads: 0
Mike Vance Mike Vance is offline Offline
Newbie Poster

Re: generating an auto number

 
0
  #3
Apr 28th, 2005
Originally Posted by nawaray
:ehh: helloo...

i have this renting application in which the user enters his name, phone , amount, date , payment to rent a flat ,,

i have a table called rent which is related to this its fields are :

name, telephone,amount, date, paid and there is one more field called serial which is the primary key, i want every user to have a different serial number ( which will be generated automatically every time i add a user)

for example:

when user one will be added he will have serial number 1

when user two is added he will have serial number 2

user 10 serial 10 and so on ,, i tried doing many things but it doesnt work ,

this is my code for the add button:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdSave_Click()
  2. adoBooks.Recordset.Fields("Name") = txtName.Text
  3. adoBooks.Recordset.Fields("telephone") = txtPhone.Text
  4. adoBooks.Recordset.Fields("amount") = txtAmount.Text
  5. adoBooks.Recordset.Fields("Date") = txtDate.Text
  6. adoBooks.Recordset.Fields("paid") = Check1.Value
  7. adoBooks.Recordset.Fields("serial") = txtSerial.Text
  8.  
  9. adoBooks.Recordset.Fields("serial") = serial
  10. adoBooks.Recordset.Update


and this is when the form is loaded:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. serial = adoBooks.Recordset.Fields("serial")
  2. serial = serial + 1

where serial is an integer

i tried doing this: ( by adding a function)
Public Function getUniqID(tblname As String, fldname As String) As Long
    Dim tmpRs As ADODB.Recordset
    Dim MaxID As Long
Set tmpRs = New ADODB.Recordset
 tmpRs.CursorLocation = adUseClient
   tmpRs.Open "select max(" & fldname & ") as maxID from " & tblname ', Cnn, adOpenDynamic, adLockOptimistic
   If IsNull(tmpRs("maxID")) = True Then 
MaxID = 0 
Else MaxID = tmpRs("maxID")
  If MaxID = 0 Then
   intSerialNo = 1  
 Else
    intSerialNo = MaxID + 1
    End If
    tmpRs.Close
End Function


Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2.  
  3. Call getUniqID("rent", "serial")
  4.  
  5. End Sub

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub cmdSave_Click()
  2.  
  3. Call getUniqID("rent", "serial")
  4.  
  5. adoBooks.Recordset.Fields("Name") = txtName.Text
  6. adoBooks.Recordset.Fields("telephone") = txtPhone.Text
  7. adoBooks.Recordset.Fields("amount") = txtAmount.Text
  8. adoBooks.Recordset.Fields("Date") = txtDate.Text
  9. adoBooks.Recordset.Fields("paid") = Check1.Value
  10.  
  11. adoBooks.Recordset.Fields("serial") = intSerialNo
  12. adoBooks.Recordset.Update
  13.  
  14. End Sub

but i keep on getting an error in the line colored red
any help please?

nawaray
Why can't you change the MS Access data type to Autonumber for your serial field. When you add a record the serial number will be created for you automatically and always Uniquely. After the execution of adoBooks.Recordset.Update you can assign a text field like txt_Serial.text = adoBooks.Recordset.fields("serial") or txt_Serial.text =adoBooks.Recordset!serial thus returning the serial number from MSAccess. If you ever switch to SQL Server you must set .CursorLocation to get the new serial number.

Mike
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC