Results 1 to 10 of 10
  1. #1
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA for Access (A2002)

    Hi,

    I have a question about this code:

    Option Compare Database
    Option Explicit

    Dim iCounter As Integer
    Dim Letter1 As String
    Dim Letter2 As String
    Dim Letter3 As String
    Dim Letter4 As String
    Dim Letter5 As String
    Dim Letter6 As String

    Private Sub cmdA_Click()
    Select Case txtA.Value
    Case "A", "a"
    Letter1 = "A"
    If (Letter1 & Letter2 & Letter3 & Letter4 & Letter5 & Letter6 = "ACCESS" Then
    MsgBox "You Won!", , "Hangman"
    txtA.Enabled = False
    txtC1.Enabled = False
    txtC2.Enabled = False
    txtE.Enabled = False
    txtS1.Enabled = False
    txtS2.Enabled = False
    Else
    txtA.Enabled = False
    End If

    Case Else
    If imgLeftLeg.Visible = False Then
    imgLeftLeg.Visible = True
    iCounter = iCounter + 1
    ElseIf imgRightLeg.Visible = False Then
    imgRightLeg.Visible = True
    iCounter = iCounter + 1
    ElseIf imgBody.Visible = False Then
    imgBody.Visible = True
    iCounter = iCounter + 1
    ElseIf imgLeftArm.Visible = False Then
    imgLeftArm.Visible = True
    iCounter = iCounter + 1
    ElseIf imgRightArm.Visible = False Then
    imgRightArm.Visible = True
    iCounter = iCounter + 1
    ElseIf imgHead.Visible = False Then
    imgHead.Visible = True
    iCounter = iCounter + 1
    End If

    If iCounter = 6 Then
    MsgBox "Sorry, you lost.", , "Hangman"
    txtA.Enabled = False
    txtC1.Enabled = False
    txtC2.Enabled = False
    txtE.Enabled = False
    txtS1.Enabled = False
    txtS2.Enabled = False
    End If

    End Select

    End Sub

    First, the reference to iCounter in the variable declaration-To what does this refer? I know what it's supposed to do, but I'm unclear as to whether or not this is in reference to a control of some kind. In the Dim Letter1 As String variable, I can see from further code that Letter1 = A, and so on..., but not so with iCounter.

    This is just the code for one command button on my form. There are a total of six which represent each correct letter of the right answer (Access).

    Second question, If I play the game and win OR lose, the message box's don't come up as they are supposed to.

    Please help!

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

    Re: VBA for Access (A2002)

    Hi Gabi,

    The declaration "Dim iCounter As Integer" is in the declaration section of the module, so the variable iCounter is available to all procedures and functions in the module (as are Letter1 through Letter6). It is a numeric variable that is used to hold the number of tries up to now. It doesn't refer to any control of the form, it's a counter used only by the code to keep track of how many guesses you've had.

    If you click the command button cmdA, the code checks to see whether you entered the correct letter ("a" or "A") into the text box txtA.

    If so, it checks whether you guessed the entire word "ACCESS"; if you did, you won and the game is finished, otherwise, the text box txtA is disabled (you can't change it any more).

    If you didn't get the letter right, it checks which part of the hangman should be drawn (made visible), and increases the number of tries by one: iCounter = iCounter + 1.

    Finally, if iCounter has reached 6, you've used up your guesses and you lost the game.

    Do you reset all variables in the On Load or On Open event of the form? Although strictly speaking, this should not be necessary, it is good practice:

    Private Sub Form_Load()
    iCounter = 0
    Letter1 = ""
    Letter2 = ""
    Letter3 = ""
    Letter4 = ""
    Letter5 = ""
    Letter6 = ""
    End Sub

  3. #3
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Access (A2002)

    <<Do you reset all variables in the On Load or On Open event of the form? Although strictly speaking, this should not be necessary, it is good practice:>>

    Yes, that is also in the code.

    Thanks for your explanation-I'm having a bit of trouble understanding the concept of variables. I guess what I'm wondering is how does the computer know what iCounter, or Letter1, is supposed to do if it's not stated? (Or any variable, for that matter) Is it part of "built in" knowledge of the program?

    What about the message boxs?

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

    Re: VBA for Access (A2002)

    Gabi,

    The computer (Access, VBA) doesn't have the faintest idea what iCounter stands for or what it is supposed to do. The only thing it can do is check whether the you use it the way you declare it. For example, if you have a statement

    iCounter = "Gabi"

    you will get an error message, not because the computer dislikes your name, but because you told it that iCounter is an integer. But if you set

    iCounter = -37

    which is obviously nonsense since iCounter is the number of guesses you have used, the computer won't bat an eyelash. A value of -37 is valid for an integer, and that is all the computer looks out for. So you (the programmer) are responsible for giving a variable "meaning" and to use it in a meaningful way.

    The code looks OK, without seeing all of it, it is impossible to know if and why the message boxes don't appear at the right times.

  5. #5
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Access (A2002)

    <<So you (the programmer) >> <img src=/S/blush.gif border=0 alt=blush width=15 height=15> Hmmm, not so sure about that!!

    Re: the message box's-I'll review the rest of the code-I probably just didn't type it in correctly.

    Thanks again for the explanation on the variables, that helps tremendously.

  6. #6
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Access (A2002)

    Hans,

    Chapter 4 of my book is about looping structures. Unfortunately it does not give a definition or example of what loops are actually for. Can you give me a brief definition simple explanation?

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

    Re: VBA for Access (A2002)

    Hi Gabi,

    Looping structures are meant to take the tedium out of repeating the same piece of code several times.

    Let's take a stupid example.

    Somebody asks you to add the whole numbers 1, 2, 3, and 4 together. That's not so difficult: you start with nothing, add 1 (giving 1), then add 2 (giving 3), next add 3 (giving 6), and finally add 4 (giving the answer 10). (See note at end)

    Next, they ask you to add the whole numbers 1 through 10. You could proceed the same way, but it's starting to get tedious (the answer is 55, by the way.)

    Now, you're asked to add the whole numbers 1 through 100. Yeah, right. Although the recipe is simple (keep on adding the next number until you reach the last one), this is not something we want to write out. Let's get out our loops. In this case, we are going to add the numbers 1, 2, 3 etc. Visual Basic has the following loop structure for this:

    For i = 1 To 100
    ' add i to what we already have
    Next i

    This means: start with i=1, do something with it, then take i=2, etc. and continue until i=100 (inclusive).

    More in detail:

    <img src=/w3timages/blueline.gif width=33% height=2>

    Dim lngSum As Long
    Dim i As Long

    ' Start with zero
    lngSum = 0

    ' Let i vary from 1 through 100
    For i = 1 To 100
    ' Add i to the sum we already have
    lngSum = lngSum + i
    Next i

    ' Report the sum
    MsgBox "The sum is " & lngSum

    <img src=/w3timages/blueline.gif width=33% height=2>

    There are also other types of loops where you don't know in advance how many times you are going to repeat your instructions, either you continue while a condition is true:

    Do While condition
    ' instructions here
    Loop

    or you continue until a condition is met:

    Do
    ' instructions here
    Loop Until condition

    I'm sure that your book has examples of all of these. In all cases, the idea is to avoid the tedium of writing out a lot of repeating instructions.

    Note for the mathematically oriented reader: yes, I know that there are much more efficient ways to calculate this, but that's not the point of this exercise.

  8. #8
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Access (A2002)

    Thanks so much Hans! You should write a book <img src=/S/clever.gif border=0 alt=clever width=15 height=15> !

    On another note, are AddItem and RemoveItem not valid methods of List and Comboboxes in A2k? My book is based on A2002, but I'm working on A2k, and when I use these in code I get the error message "method or data member not found" with either AddItem or RemoveItem highlighted in the background behind the error message. I've gone through the option list and neither of them appear, but nothing seeming to mean the same thing does either.

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

    Re: VBA for Access (A2002)

    Yes, AddItem and RemoveItem were introduced in Access 2002 VBA. They don't exist in Access 2000. You'll have to skip exercises using these methods.

  10. #10
    3 Star Lounger
    Join Date
    May 2003
    Location
    Sacramento, California, USA
    Posts
    310
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Access (A2002)

    Well, at least it wasn't just me for once!! <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

Posting Permissions

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