-1

I am new to VBA and VBS. I was trying to implement a version control for my Access and stumbled upon this StackOverflow post. I googled as much as I could to understand the code and now I understand about 50 percent of the code. I don't want to ask how the entire code works because I should figure it out myself. However, there are parts which I did not find in google.

Why are these lines of code necessary?

const acForm = 2
const acModule = 5
const acMacro = 4
const acReport = 3

P.S. I have successfully implemented this code. I just wanted to understand a bit more.

Erik A
  • 31,639
  • 12
  • 42
  • 67
greenn
  • 309
  • 1
  • 6
  • 18
  • 4
    They are `constants` used to represent an `AcObjectType` in the `SaveAsText` method call. In this case using constants makes the code cleaner and easier to read. You could just as easily type the numbers into the `SaveAsText` method call but it is less maintainable etc. – Alex P Nov 09 '18 at 11:36
  • @AlexP That is a better answer! – QHarr Nov 09 '18 at 11:37
  • Also see this reference on [AcObjectType](https://learn.microsoft.com/en-us/office/vba/api/access.acobjecttype) – Alex P Nov 09 '18 at 11:43
  • 5
    If this code were to run in Access VBA, the constants declarations wouldn't be necessary, because they are inbuilt in Access. But VBScript doesn't know them. – Andre Nov 09 '18 at 11:43

1 Answers1

3

Const is a statement

Declares constants for use in place of literal values.

They don't change value. In this case the variable name is acForm and its value remains 2 throughout your program and cannot be changed while the script is running.

Wherever you see acForm it means the value 2.

When using VBA, these values are usually initialized by setting a reference, in this case, to the Microsoft Access Object Library, and that reference is pre-set when using Access. But when writing a VBScript file, you can't set references, so you need to provide any constant or enum you want to use yourself.

Better explanation from @Lankymart: You can't change a Constant value programmatically.

user692942
  • 16,398
  • 7
  • 76
  • 175
QHarr
  • 83,427
  • 12
  • 54
  • 101