Results 1 to 8 of 8
  1. #1
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046

    Error "Duplicate declaration in current scope"

    Experts:



    In my VBA code, I'm coming across a fundamental error (i.e., "Duplicate declaration in current scope"); yet I have not been able to successfully resolve it.

    Below is my code:

    Code:
        'Declare variables
        Dim rs As DAO.Recordset, sTarget As String, sField As String
        Dim sCurrent As String, i As Integer
        Dim sSQL As String, sFieldAlias As String
           
        'Determine which version format
        Select Case Forms!F01_MainMenu!optVersionType
            
            'PK Version:
            Case 1
                   
                Const conSQL As String = "SELECT [00_tbl_Master_PK].SOURCE_FILE, CASE_NUMBER, |FIELDS_HERE| FROM [00_tbl_Master_PK];"
                
                'Reset counter
                iCounter = 0
                
                'Delete all queries
                If Forms![F01_MainMenu].chkDeleteAllQry = True Then Call DeleteAllQueries
                
                Set rs = CurrentDb.OpenRecordset("SELECT * FROM 01_tbl_ProductTables_PK Order BY PRODUCT_TABLE, AFSAS_FIELDNAME;", dbOpenSnapshot)
           
            'String Version:
            Case 2
               
                Const conSQL As String = "SELECT [00_tbl_Master_String].SOURCE_FILE, CASE_NUMBER, |FIELDS_HERE| FROM [00_tbl_Master_String];"
                
                'Reset counter
                iCounter = 0
                
                'Delete all queries
                If Forms![F01_MainMenu].chkDeleteAllQry = True Then Call DeleteAllQueries
                
                Set rs = CurrentDb.OpenRecordset("SELECT * FROM 01_tbl_ProductTables_String Order BY PRODUCT_TABLE, AFSAS_FIELDNAME;", dbOpenSnapshot)
           
        End Select
    By default, the option group's value is = 1. Presently, however, it does NOT matter which option I'm choosing... I always end up w/ the same error.

    My question: How can the CONST reference be modified so that when I leave the CASE statement I pass the correct conSQL value into the remainder of the code?

    Thanks,
    Tom
    Attached Thumbnails Attached Thumbnails Const.JPG  

  2. #2
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    Use a string variable. I don't see the sense in declaring a constant that isn't.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Minty is online now VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    @Micron is correct.

    Oddly this isn't the first time I've seen really weird use of the Const declaration type.
    I wonder if there is a "guide" somewhere that has some dodgy practice in it that is influencing how newer users are writing things.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    You only need to declare once.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726

  6. #6
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,801
    From that link: "After a constant is declared, it cannot be modified or assigned a new value."
    Each version posted has a different sql statement so I don't see Const is of any use in that code no matter how many times it's declared. If one must do things the hard way, then 2 constants would be required and given the proper scope for the task.
    Last edited by Micron; 05-20-2021 at 04:31 PM. Reason: added comment
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Agree 100% with micron. When you change values it is no longer a Constant.

  8. #8
    skydivetom is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Feb 2019
    Posts
    1,046
    Thank you... that worked great... 'appreciate the info. Cheers!

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

Similar Threads

  1. Replies: 5
    Last Post: 04-08-2021, 09:15 AM
  2. Unexpected error 3021, "No Current Record"
    By GraeagleBill in forum Programming
    Replies: 22
    Last Post: 02-23-2021, 08:27 PM
  3. Replies: 3
    Last Post: 09-21-2017, 05:40 AM
  4. Form error "the current field must match..."
    By plavookins in forum Forms
    Replies: 0
    Last Post: 04-13-2011, 07:42 AM
  5. "No Current Record" error...not sure why it is there
    By jgelpi16 in forum Programming
    Replies: 1
    Last Post: 01-25-2011, 10:14 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