Share via

compile error: expected array

Anonymous
2015-02-24T22:48:02+00:00

Hi everyone I was hoping someone can advice me on why I'm getting this error on my code, Thank you:

Dim intItems As Integer

Dim strFirst() As String, strLast() As String, strName As String

Private Sub Form_Load()

'launch inputbox and assign value

intItems = Val(InputBox("How many names?", "Name Count"))

'force user to provide number

Do While intItems <= 0

MsgBox "Please enter a number greater then 0.", vbCritical, "Correct Value Required"

'keep launching inputbox until user provides number

intItems = Val(InputBox("How many names?", "Name Count"))

Loop

'now that we know the number of elements needed,

'the dynamic arays can be determined

ReDim strFirst(intItems - 1), strLast(intItems - 1)

'update status so user knows the latest

Call Status

End Sub

Private Sub Status()

 Dim intI As Integer, intCount As Integer

'loop through one array and count only those elements that have data

For intI = 0 To UBound(strName)

If strName(intI) <> "" Then

intCount = intCount + 1

End If

Next intI

Exit For

End Sub

Private Sub cmdAddName_Click()

Dim intI As Integer

'validate input

If IsNull(txtFirst.Value) Or IsNull(txtLast.Value) Then

MsgBox "There must be values for both First and Last Names.", _

vbCritical, "Data Entry Error"

Else

'check to see if array is full

If strFirst(intItems - 1) <> "" Then

MsgBox "Sorry no room!"

'reset values of textboxes

Me.txtFirst.Value = Null

Me.txtLast.Value = Null

Else

'If not full then setup to assign 3 values to 3 array values

For intI = 0 To intItems - 1

'before assigning a value, check to see

'if an element already contains a value

If strFirst(intI) = "" Then

'If one array element is empty then the other must be too, so proceed

strFirst(intI) = Me.txtFirst.Value

strLast(intI) = Me.txtLast.Value

Me.txtFirst.Value = Null

Me.txtLast.Value = Null

'the unassign array elements have been found

'and values assign so exit the loop

Exit For

End If

Next

End If

End If

'update status so user knows the latest

Call Status

End Sub

Private Sub cmdCancel_Click()

'clear the elements of values

ReDim strFirst(intItems - 1), strLast(intItems - 1)

'clear display

End Sub

Private Sub cmdDisplayList_Click()

Dim intI As Integer, strOutPut As String

'check to see if at least the first of one element has a value

If strFirst(0) = "" Then

MsgBox "Nothing to display", vbInformation, "No Data"

Me.txtFirst.Value = Null

Me.txtLast.Value = Null

Else

'if so then loop throug array

For intI = 0 To UBound(strName)

'only were the variables contain data should concatenate the output

If strLast(intI) <> "" Then

strOutPut = strOutPut & strFirst(intI) & " " & _

strLast(intI) & vbCrLf

End If

Next intI

Me.lblOutPut.Caption = strOutPut

'clear text boxes

Me.txtName.Value = Null

Me.txtBirthDate.Value = Null

End Sub

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. HansV 462.6K Reputation points MVP Volunteer Moderator
    2015-02-24T23:12:33+00:00

    You should have

    Dim strFirst() As String, strLast() As String, strName**()** As String

    but the code still won't do anything useful since you don't populate the array strName anywhere in the code.

    You should also remove the line Exit For from the Status procedure.

    The procedure cmdDisplayList_Click is missing an End If just above End Sub.

    0 comments No comments