Array Mismatch

musclecarlover07

Registered User.
Local time
Today, 12:03
Joined
May 4, 2012
Messages
236
I have an array that is not working :confused:

Code:
    Dim Utype As Variant
    Utype = Array(3, 4, 8, 13, 14)

    If Utype = Me.UpgradeType Then
        MsgBox "working"
    Else
        MsgBox "not working"
    End If

So basically When I open the form to a specific record I want certain fields to be hidden. To determine if the fields are hidden or not are the values I have in the array. So example if I open record 10 and in the text field UpgradeType equals to any of the following 3, 4, 8, 13, 14 then I want other fields to hide.

If it equals anything else then I don't want anything to happen. But every time I rut the above code it gives me error mismatch and highlights
Code:
If Utype = Me.UpgradeType Then

Not sure whats wrong. When ever it gives me the error I hit debug and then over over Utype and it says 13 and then hover over Me.UpgradeType and it says 13 :banghead:
 
You've created an array you need to refer to an element in the array, e.g. Utype(0). Loop through the array if necessary.
 
I have never worked with an array. I tired what you said Utype(0) and it worked. I got that part figured out. I tried looking up several places for array looping and that did not help very much. :(
 
Ok, that helps a great deal. Here is what I have came up with:
Code:
    Dim Utype As Variant
    Dim Upgrades As String
    Dim lngPosition As Long
    
    Utype = Array(3, 4, 8, 13, 14)
    
    For lngPosition = LBound(Utype) To UBound(Utype)
        If Utype(lngPosition) = Me.UpgradeType Then
            Me.AttackDice.Visible = True
            Me.Range.Visible = True
            GoTo ExitME:
        Else
            Me.AttackDice.Visible = False
            Me.Range.Visible = False
        End If
    Next lngPosition

ExitME:
So far it works perfectly. :D
 
It looks alright but there are a couple of things you should change or do differently:

1. To exit a For loop use Exit For. Don't use GoTo commands in this fashion.
2. Use a boolean variable inside the loop and set it to True if the condition is met, after the entire loop use the boolean variable to set the Visible properties of the controls.

However, I don't know what your main objective of using a loop is. From what I can see I think things can be done differently.
 
Ok. For the number 2 I'm not sure how to do all that. I figured this would have been the simplest solution in the sense of less code to type.

so basically when the user clicks to open the form then certain fields hide (AttackDice and Range) So if the upgradeType has the values (3,4,8,13,14) Then is shows the 2 fields. If it is anything else from 1-15 then it hides the fields. I hope that makes sense.
 
Ok, for your satisfaction:
Code:
    Dim Utype       As Variant
    Dim Upgrades    As String
    Dim lngPosition As Long
    Dim blHide      As Boolean
    
    Utype = Array(3, 4, 8, 13, 14)
    
    For lngPosition = LBound(Utype) To UBound(Utype)
        If Utype(lngPosition) = Me.UpgradeType Then
            blHide = True
            Exit For
        End If
    Next
    
    Me.AttackDice.Visible = blHide
    Me.Range.Visible = blHide

Show me a screenshot of the form pointing out the relevant fields/controls you're talking about.
 
Ok. So i think I have gotten what you wanted.

The top picture is supposed to be what hides the Attack Dice and the Range fields. With your suggestion and what I came with the labels never hide so I have to add that bit in as well.

The bottom picture is when they need to be visible.
 

Attachments

I didn't follow what you said here.
The top picture is supposed to be what hides the Attack Dice and the Range fields. With your suggestion and what I came with the labels never hide so I have to add that bit in as well.

The UpgradeType values are in a table of their own right? Add an extra Yes/No field that would indicate which value would hide the two textboxes. Use the new value to set the visible property of the textboxes.
 
Out of interest, is there any reason why it would be better to use an array rather than a select case for this? Something like this...

Code:
 Select Case Me.UpgradeType
    Case 3, 4, 8, 13, 14
        'hide unhide what you want here
    
    Case Else
        'What ever here
 End Select

Or am I missing what your trying to achieve here?
 
vbaInet
Yes the upgrade values are in their own table.

So the top picture the fields are supposed to be hidden. It hid the fields but not the corresponding labels did not hide. I had to add the extra line for the labels for some odd reason.

Acropolis
Your not missing anything. I thought the array was the easiest/best way. I'm open to suggestions on what the easiest/best way is.
 
It works exactly like all other suggestions. The labels don 't hide Its odd. Normally when I have a field hide the associated label hides but with this it doesn't.
 
If it were me, I would go with the select case, seems a lot easier to do.


Code:
  Select Case Me.UpgradeType
    Case 3, 4, 8, 13, 14
        'hide unhide what you want here 
         Me.AttackDice.Visible = True
        Me.Range.Visible = True
         Me.RangeLabel.visible = True
         Me.AttackDiceLabel.Visible = True    
    Case Else
        'What ever here
        Me.AttackDice.Visible = False
        Me.Range.Visible = False
         Me.RangeLabel.visible = False
         Me.AttackDiceLabel.Visible = False
 End Select


Me.AttackDice.Visible = blHide
Me.Range.Visible = blHide
 
vbaInet
Yes the upgrade values are in their own table.

So the top picture the fields are supposed to be hidden. It hid the fields but not the corresponding labels did not hide. I had to add the extra line for the labels for some odd reason.
So create another field for this and you need to hide the associated label as well.

Whether it's a Select Case statement or an array it doesn't really matter in this case. You're referring to field values so it needs to be set up in the table as well. All the logic should stay in the table not in code.
 
Well thanks everyone this was a great help. vbaInet thanks again for that link. It was a great read.
 

Users who are viewing this thread

Back
Top Bottom