Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2019
    Posts
    1,045

    "Const strPath As String = "C:\Users\..\" results in error (after adding radio buttons)

    Hello:

    I use a VBA routine to import Excel files into an Access table. As of right now, the file path (where XLSX files are stored), is hard-coded. I want it to remain this way!

    See below where I define the constant value for the path. It works great!

    Code:
    Const strPath As String = "C:\Users\..\Import_Files_All\"
    The only modification is that I now want to import different XLSX files based on a selected radio button (value).
    To accomplish this, I added string "strWhichPath".

    Next, depending on the radio button, I intend to pass that value into the *Const strPath*.

    Unfortunately, I now get an error indicating a compile error (see attached JPG).

    Code:
    Dim strWhichPath As String
    
    Select Case Forms![FormName].optValidationRecordSet.Value
    
        'Append all records
        Case "1":
            strWhichPath = "Import_Files_All"                  
    
        'Append record subset #1
        Case "2":
            strWhichPath = "Import_Files_Subset1"                 
    
        'Append record subset #2
        Case "3":
            strWhichPath = "Import_Files_Subset2"                 
    
    End Select
    
    Const strPath As String = "C:\Users\..\" & strWhichPath & "\"
    My question: How do I need to modify the Const strPath OR SELECT CASE in order to pass the variable into it AND to not receive the compile error?
    Attached Thumbnails Attached Thumbnails CompileError.JPG  

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You can't use a variable in a constant declaration - the clue is in the name - It's a Constant, so it cant vary.
    You can use it as a basis to build another path though

    Dim MyConstructedPath as String

    MyConstructedPath = StrPath & strWhichPath
    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 ↓↓

  3. #3
    Join Date
    Feb 2019
    Posts
    1,045
    Minty:

    Perfect!!! That did the trick!

    Your help is much appreciated.

    Cheers,
    Tom

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

Similar Threads

  1. Replies: 28
    Last Post: 03-23-2021, 11:02 AM
  2. Replies: 3
    Last Post: 06-06-2018, 08:26 PM
  3. Replies: 7
    Last Post: 08-07-2017, 03:22 PM
  4. Replies: 1
    Last Post: 09-07-2015, 08:00 AM
  5. Replies: 1
    Last Post: 09-03-2014, 03:27 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