Results 1 to 5 of 5
  1. #1
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398

    Coding within ADO

    I can't figure out why this isn't working. Within table PC_LOG, a text field is named [Request Number]. An example is FY14-099. What I'm trying to do is increase the numerial portion by 1. I first have to confirm I'm within the correct fiscal year, ie: FY14, then look for the MAX numeric part of [Request Number]. When I run the following code, it exits out of the sub when trying to do the msgbox rst.Fields(0)

    Dim FiscalYear As Integer
    Dim MaxRequestNumber As String
    'Request Number is based on fiscal year, and a numeric count within that year
    'Fiscal year is from July to June

    'Establish correct year, and assign to variable FiscalYear
    If DatePart("m", Date) > 6 Then
    FiscalYear = Right(DatePart("yyyy", Date), 2) + 1
    Else
    FiscalYear = Right(DatePart("yyyy", Date), 2)
    End If

    'SELECT Max(Right([request number],3)) AS MaxNumber FROM PC_LOG WHERE (((PC_LOG.[Request Number]) Like "FY14*"))
    strSQL = "SELECT Max(Right(PC_LOG.[Request Number],3)) AS MaxNumber FROM PC_LOG WHERE (((PC_LOG.[Request Number]) Like "
    strSQL = strSQL & Chr(34) & "FY" & FiscalYear & "*" & Chr(34) & "))"



    Set rst = New ADODB.Recordset
    rst.Open strSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly

    MsgBox rst.Fields(0)

    rst.Close
    Set rst = Nothing

    I've confirmed the recordset only has 1 record. Thank you in advance!!

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Add the blue line as shown and single step through the code.
    Code:
    'SELECT Max(Right([request number],3)) AS MaxNumber FROM PC_LOG WHERE (((PC_LOG.[Request Number]) Like "FY14*"))
    strSQL = "SELECT Max(Right(PC_LOG.[Request Number],3)) AS MaxNumber FROM PC_LOG WHERE (((PC_LOG.[Request Number]) Like "
    strSQL = strSQL & Chr(34) & "FY" & FiscalYear & "*" & Chr(34) & "))"
    
    Debug.Print strSQL
    Is the SQL formed correctly?
    Can you copy the line from the immediate window, create a new query, paste in the SQL and execute the query?
    Are the results what you expect?

  3. #3
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    I think I figured it out. Within my strSQL, I'm using MAX from the query wizard. I've been reading that MAX is not a function within VBA. What do you think?

  4. #4
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    You are correct. Their is a problem within the strSQL statement using the Like option. I can't figure out what I've typed incorrectly. Here's what I'm testing with.

    strSQL = "SELECT PC_LOG.[Request Number] FROM PC_LOG WHERE (((PC_LOG.[Request Number]) Like "
    strSQL = strSQL & Chr(34) & "FY14*" & Chr(34) & "))"

  5. #5
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    The problem was using the wildcard of "*" rather than "%" Everything worked fine after changing the wildcard. Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Coding for Sum in vba
    By glen in forum Programming
    Replies: 14
    Last Post: 12-20-2012, 05:10 PM
  2. VBA for colour coding
    By SWG in forum Forms
    Replies: 1
    Last Post: 08-29-2012, 05:20 PM
  3. Please help with VB coding
    By winterh in forum Import/Export Data
    Replies: 11
    Last Post: 03-19-2012, 06:05 PM
  4. Need help in VBA coding
    By Kcgp in forum Programming
    Replies: 6
    Last Post: 02-01-2012, 11:22 PM
  5. Bar-Coding
    By texasprincess7 in forum Access
    Replies: 1
    Last Post: 02-12-2009, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums