Results 1 to 18 of 18

Thread: How to build a Count Querie for VBA code

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    How to build a Count Querie for VBA code

    I'm fixing a software from an old source code. the code is here but the query in the database is not there anymore. I just want the software to keep adding a number every time i input a two customers with the same name. This is the code i have on the source


    Private Sub cmdCheck_Click()
    Dim q As QueryDef
    Dim rs As Recordset
    Dim CountStr As String
    Dim Count As Long
    If Len(txtFirstName) = 0 Then
    MsgBox "Please enter a first name", vbCritical, "F-Miracle"
    Exit Sub
    End If

    If Len(txtLastName) = 0 Then
    MsgBox "Please enter a last name", vbCritical, "F-Miracle"
    Exit Sub
    End If


    Set q = CurrentDB.QueryDefs("int_qGetCodeCount")
    q.Parameters("pCode") = UCase(MakeCode)
    Set rs = q.OpenRecordset

    If rs.RecordCount = 0 Then
    Count = 1
    Else
    Count = rs("MaxCode") + 1
    End If
    CountStr = Format(Count, "000")
    txtCode = UCase(MakeCode) & CountStr
    cmdSave.Enabled = True

    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to build a Count Querie for VBA code

    what is makecode?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    I dont really know what it means but i think is the first name and last name

    Wilfredo Romero

    ROMEWIL

    and i want to add the counter at the end

    ROMEWIL001

    and if i add a new client with the same name, I want the software to give me this

    ROMEWIL002

    and so on. Thank you very much
    Last edited by war6000; Mar 22nd, 2010 at 12:30 PM. Reason: I typed the wrong thing first

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to build a Count Querie for VBA code

    i am not experienced with parameters, but i think it would need to be something like
    q.Parameters("pCode") Like UCase(MakeCode) & "*"
    to return all records that match the makecode and any suffix
    you can then use the recordcount of the recordset to work out the next suffix
    countstr = format(rs.recordcount) + 1,"000")

    i am not sure if the parameters is correct, search on parameters like for examples, here, database forum or google
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    This is the code for the frame "addCustomer"(red part) the count section is not working. It is only adding a 001 to every customer even if the have the same name

    Option Explicit
    Public boolCancled As Boolean
    Public NewCustomerID As Long
    Public NewFirstName As String
    Public NewLastName As String
    Public NewSSN As String
    Public NewAddress As String
    Public NewTransferedDate As Date


    Private Sub cmdCancel_Click()
    boolCancled = True
    Hide
    End Sub



    Public Function MakeCode() As String
    Dim lastpart As String
    Dim firstpart As String
    Dim i As Long

    If Len(txtLastName) < 4 Then
    lastpart = txtLastName
    For i = 1 To 4 - Len(txtLastName)
    lastpart = lastpart & "0"
    Next i
    Else
    lastpart = Left(txtLastName, 4)
    End If

    If Len(txtFirstName) < 2 Then
    firstpart = Me.txtFirstName

    For i = 1 To 2 - Len(txtFirstName)
    firstpart = firstpart & "0"
    Next i
    Else
    firstpart = Left(txtFirstName, 2)
    End If

    MakeCode = lastpart & firstpart

    End Function



    Private Sub cmdCheck_Click()
    Dim q As QueryDef
    Dim rs As Recordset
    Dim CountStr As String
    Dim Count As Long
    If Len(txtFirstName) = 0 Then
    MsgBox "Please enter a first name", vbCritical, "F-Miracle"
    Exit Sub
    End If

    If Len(txtLastName) = 0 Then
    MsgBox "Please enter a last name", vbCritical, "F-Miracle"
    Exit Sub
    End If


    Set q = CurrentDB.QueryDefs("int_qGetCodeCount")
    q.Parameters("pCode") = UCase(MakeCode)
    Set rs = q.OpenRecordset

    If rs.RecordCount = 0 Then
    Count = 1
    Else
    Count = rs("MaxCode") + 1
    End If

    CountStr = Format(Count, "000")
    txtCode = UCase(MakeCode) & CountStr
    cmdSave.Enabled = True



    End Sub


    Private Sub cmdSave_Click()
    Dim rs As Recordset

    On Error Resume Next
    On Error GoTo 0
    Set rs = CurrentDB.OpenRecordset("tblCustomer")
    rs.AddNew
    rs("FirstName") = txtFirstName
    rs("LastName") = txtLastName
    rs("Code") = txtCode
    rs("Middle") = txtMiddle
    rs("DateOfTransferToLegal") = txtTransfered
    rs("EmployeeID") = CurrentEmp.EmployeeID
    rs.Update
    rs.Bookmark = rs.LastModified



    NewCustomerID = rs("CustomerID")
    NewFirstName = txtFirstName
    NewLastName = txtLastName
    NewSSN = ""
    NewAddress = ""
    NewTransferedDate = txtTransfered



    boolCancled = False
    Hide

    End Sub

    Private Sub Form_Activate()
    cmdSave.Enabled = False
    txtFirstName = ""
    txtLastName = ""
    txtCode = ""
    txtTransfered.Value = Now()
    txtMiddle = ""
    txtFirstName.SetFocus
    boolCancled = True

    End Sub

    Private Sub Form_Load()
    boolCancled = True
    End Sub



    Private Sub txtFirstName_LostFocus()
    If txtFirstName = "" Then Exit Sub

    If InStr(1, txtFirstName, " ") <> 0 Then
    MsgBox "You cannot have spaces in the first name", vbCritical, "F-Miracle"
    txtFirstName.SetFocus
    End If

    ' If txtFirstName = UCase(txtFirstName) Then
    ' MsgBox "Do not use all upper case letters!", vbCritical, "F-Miracle"
    ' txtFirstName.SetFocus
    ' End If

    txtFirstName = LCase(txtFirstName)
    txtFirstName = UCase(Left(txtFirstName, 1)) & Mid(txtFirstName, 2)

    End Sub


    Private Sub txtLastName_LostFocus()
    If txtLastName = "" Then Exit Sub
    If InStr(1, txtLastName, " ") <> 0 Then
    MsgBox "You cannot have spaces in the last name", vbCritical, "F-Miracle"
    txtLastName.SetFocus
    End If

    ' If txtLastName = UCase(txtLastName) Then
    ' MsgBox "Do not use all upper case letters!", vbCritical, "F-Miracle"
    ' txtLastName.SetFocus
    ' End If

    txtLastName = LCase(txtLastName)
    txtLastName = UCase(Left(txtLastName, 1)) & Mid(txtLastName, 2)

    End Sub
    Last edited by war6000; Mar 22nd, 2010 at 08:32 PM. Reason: code not showing up

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to build a Count Querie for VBA code

    do you check the recordset returned to see if it is not an empty recordset?
    afaik it will only return any records if there is an exact match to the parameter
    so the most it will return is 1 record

    Count = rs("MaxCode") + 1
    i have no idea what is in field maxcode, but it does not seem, to me, relevant to this operation, i would assume it would always be 0, empty or null, which is why each new name is added with 001
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    so what would you do in order to make the application do that counting on the customers that have the same name

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to build a Count Querie for VBA code

    i would use an sql statement if i was doing it

    but i don't use access, so the code is different

    something like
    vb Code:
    1. set rs = currentdatabase.openrecordset("select * from tblCustomer where pcode like '" & ucase(makecode) & "*'"
    you would need to test the results
    you can also use count, which would only return the number of records, rather than a recordset

    but if at any time, any records are deleted you could find that the next number is already used
    cust001
    cust003
    cust004
    3 names so next number would be 4, but it already exists
    Last edited by westconn1; Mar 24th, 2010 at 03:07 PM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    Where in my source code would i be able to paste or replace the line you gave me
    1.
    set rs = currentdatabase.openrecordset("select * from tblCustomer where pcode like '" & ucase(makecode) & "*'"

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to build a Count Querie for VBA code

    in place of
    Set rs = q.OpenRecordset

    unfortunately none of the access gurus have jumped in, so i am just trying to make suggestions to help, but i have not used querydef with parameters
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    I just hope an Access guru to hopefully jump on this and helps me cuz i had no luck on that last post westconn1 thank you very much. im still gonna try to figure this out. Thank you all for the help you have been giving me

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    does anyone know how to get this done. Previous post did not do the trick

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to build a Count Querie for VBA code

    @war6000

    Let's start from the scratch....

    Forget about the code that you have and tell me in simple words what do you want?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    I want the software to give each client a unique code (something easier than just numbers)

    the old version of the software use to add clients in this format




    (first 4 letters of last name) + (first 2 of first name) + (counter)

    Romero, Wilfredo

    (ROME) + (WI) + (001)

    ROMEWI001

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How to build a Count Querie for VBA code

    Fair Enough.

    1) Are all these names in the database?
    2) Would you be adding new names to the database and do you want to follow the same nomenclature for the new names that will be added later?
    3) What would be the scenario if you have names like these...

    Romeo, Wild
    Romero, Wilde
    Romero, Wilfredo
    Romeo, Wilfredo
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    Romeo, Wild
    Romero, Wilde
    Romero, Wilfredo
    Romeo, Wilfredo

    as of right now if i was to add those names the software would keep giving me the same code

    ROMEWI001

    I want the software to keep adding a number every time similar names are inserted. for example

    Romeo, Wild ROMEWI001
    Romero, Wilde ROMEWI002
    Romero, Wilfredo ROMEWI003
    Romeo, Wilfredo ROMEWI004

  17. #17
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: How to build a Count Querie for VBA code

    Quote Originally Posted by war6000 View Post
    I want the software to give each client a unique code (something easier than just numbers)

    the old version of the software use to add clients in this format




    (first 4 letters of last name) + (first 2 of first name) + (counter)

    Romero, Wilfredo

    (ROME) + (WI) + (001)

    ROMEWI001
    You're assuming that everyone has at least 4 letters in their first name.
    This might not be true.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    40

    Re: How to build a Count Querie for VBA code

    Quote Originally Posted by abhijit View Post
    You're assuming that everyone has at least 4 letters in their first name.
    This might not be true.
    Majority of our clients have but if they don't the software will add 0's to complete the entry

    John Lee
    LEE0JO001

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width