VBA/Excel/Access/Word/Language Basics/Compiler Directive

Материал из VB Эксперт
Перейти к: навигация, поиск

Use #Const to define the constant

   <source lang="vb">

  1. Const UseEventLog = True

Private Sub DebugPrint(ByVal Source As String, ByVal Message As String)

   #If UseEventLog Then
     Call EventLog.WriteEntry("Source: " & Source & " Message: " & Message)
   #Else
     Debug.Print "Source: " & Source & " Message: " & Message
   #End If

End Sub

</source>
   
  


Use #If

   <source lang="vb">

  1. Const Debugging = True

Public Sub Trap(ByVal Source As String, ByVal Message As String)

   #If Debugging Then
     Call DebugPrint(Source, Message)
     Stop
   #End If

End Sub

</source>
   
  


Use string text value as the compiler directive

   <source lang="vb">

  1. Const Language = "Spanish"

Sub cmdConditionalCompilation_Click()

  #If Language = "Spanish" Then
     msgBox "Hola, Que Tal?"
  #Else
     msgBox "Hello, How Are You?"
  #End If

End Sub

</source>
   
  


With the compiler directive

   <source lang="vb">

  1. Const Language = 1

Sub ConditionalIf()

  #If Language = 1 Then
     msgBox "Hola, Que Tal?"
  #Else
     msgBox "Hello, How Are You?"
  #End If

End Sub

</source>