Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Compilation Constants (2003)

    How do you set a compilation constant at compile time?
    I mean, if I wanted to set the AllFiles constant in the following sub to true, where should I set it?

    Sub FileSearch3XP()
    Dim sngStart As Double
    Dim sngEnd As Double
    Dim int1 As Integer
    Dim str1 As String
    #Const AllFiles = False

    'Search in a folder on linked computer
    'for files containing ADO
    With Application.FileSearch
    'Start a new search
    .NewSearch
    'Set search criteria
    .LookIn = "cab2000cBooksArchiveAccess11"
    .SearchSubFolders = False
    'When searching for text, consider
    'restricting the files you search
    '*.* can require more time
    'than msoFileTypeWordDocuments
    #If AllFiles = True Then
    .FileName = "*.*"
    #Else
    .FileType = msoFileTypeWordDocuments
    #End If
    .TextOrProperty = "ADO"
    End With

    With Application.FileSearch
    'Execute the search
    sngStart = Now
    If .Execute() > 0 Then
    sngEnd = Now
    Debug.Print "The file search took " & _
    DateDiff("s", sngStart, sngEnd) & " seconds."
    str1 = "There were " & .FoundFiles.Count & _
    " file(s) found. Do you want to see " & _
    "them in a series of messages boxes?"
    If MsgBox(str1, vbYesNo) = vbYes Then
    'Display names of all found files
    For int1 = 1 To .FoundFiles.Count
    MsgBox .FoundFiles(int1)
    Next int1
    End If
    Else
    'If no files found, say so
    MsgBox "There were no files found."
    End If
    End With

    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    I don't understand the question. In the first place, conditional compiler constants are set manually by the programmer, while designing the code. In the second place, they are constants, as the name indicates, their value cannot be modified.

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    Hi Hans,
    I've found this sub in a book about Access. As it stands now, the search the sub performs is for a specific kind of file(.FileType = msoFileTypeWordDocuments) because the AllFiles constant is set to False.
    but what if you want to search for all files(.FileName = "*.*")?
    How do you set the AllFiles constant to True?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    Change the line

    #Const AllFiles = False

    to

    #Const AllFiles = True

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    So what's the difference between a compilation constant and a normal constant?

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    You can use a conditional compiler constant for example to avoid problems with different versions of Access. You can create a version of your database for Access 97, and one for Access 2000 and higher, for example. Normally, having Access 2000 code in an Access 97 database will lead to error messages. Instead of having different versions of the code in the two databases, you can use the same code, and enable the appropriate part using a compiler constant. The only difference between the code in the two versions is the value of the compiler constant.

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    Thank you Hans,
    In the example you've just given, I guess you'd need to set the value of the compilation constant based on what version of Access you're using to open the mdb file. How would you go about doing that?

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    You would create two copies of the database, then manually change the line that sets the value of the compilation constant in one of the copies. Apart from that line, the databases would be identical. Give each user (a copy of) the database intended for his/her version of Access.

  9. #9
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    Wouldn't a normal constant serve the purpose equally well?

  10. #10
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    No, for the user would get a compile error. Let's take a simple (and fairly useless) example: in Access 2002, a new method AddItem was introduced for list boxes and combo boxes whose Row Source Type has been set to Value List. So you can use

    Me.ListBox1.AddItem "My Item"

    whereas in earlier versions, you had to use

    Me.Listbox1.RowSource = Me.ListBox1.RowSource & ";My Item"

    The following code will work in Access 2002/2003 but not in Access 2000:

    If Application.Version = "9.0" Then
    Me.Listbox1.RowSource = Me.ListBox1.RowSource & ";My Item"
    Else
    Me.ListBox1.AddItem "My Item"
    End If

    Even though the line with AddItem wouldn't be executed in Access 2000, it still causes a compile error, since AddItem is not known. But if you use a conditional compile constant:

    #Const Acc2000 = True

    #If Acc2000 Then
    Me.Listbox1.RowSource = Me.ListBox1.RowSource & ";My Item"
    #Else
    Me.ListBox1.AddItem "My Item"
    #End If

    the Access 2002/2003 code is hidden from the VBA interpreter in Access 2000, it doesn't "see" the line with AddItem.

  11. #11
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Alzano Lombardo, Italy
    Posts
    1,483
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    Fantastically educational explanation! Thank you Hans <img src=/S/clapping.gif border=0 alt=clapping width=19 height=23> <img src=/S/bravo.gif border=0 alt=bravo width=16 height=30>

    Going back to the Sub FileSearch3XP() at the start of the thread, it doesn't seem to me that substituting a normal constant for the AllFiles compilation constant would cause a compilation error so is there some other rationale for using a compilation constant in that case?

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    In that code, I don't see a particular reason for using a conditional compile constant, you might as well use an ordinary constant or a variable.

  13. #13
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    DB To Run On Different Platforms?
    a2k (9.0.6926) SP-3 Jet 4.0 SP-8

    I develop apps on A2k and deliver to A2002 and A2003.

    I

  14. #14
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Compilation Constants (2003)

    What is GetVersion?

  15. #15
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Niagara Falls, New York, USA
    Posts
    1,878
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Compilation Constants (2003)

    GetVersion = SysCmd(acSysCmdAccessVer)

    This was a bad example

    My question is with the above post, can you mix A2003 commands in a single A2k db using conditional compile?

    Thanks, John

Page 1 of 2 12 LastLast

Posting Permissions

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