PC Review


Reply
Thread Tools Rate Thread

Array compile error "Expected: As"

 
 
eschloss
Guest
Posts: n/a
 
      9th Oct 2008
Access 2003

I do not understand why I receive an error for the indicated line below.
Please let me know my syntax error.

Private Sub Form_Open(Cancel As Integer)

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Dim name() As String
Dim i As Integer
i = 0
sql = "SELECT L_tblArea_Tech.L_Tech " & _
"FROM L_tblArea_Tech " & _
"WHERE L_tblArea_Tech.L_Area = 'Q/A' And L_tblArea_Tech.L_Tech_Current
= Yes " & _
"ORDER BY L_tblArea_Tech.L_Tech;"
Set db = CurrentDb()
Set rst = db.OpenRecordset(sql)
rst.MoveFirst

Do While Not rst.EOF
i = i + 1
ReDim name(1 To i) As String
name(i) = rst!L_Tech <-----------Error
here----------------
rst.MoveNext
Loop

ReDim name(1) As String
rst.Close
Set rst = Nothing

MsgBox name7

End Sub
 
Reply With Quote
 
 
 
 
Jim Burke in Novi
Guest
Posts: n/a
 
      9th Oct 2008
Not sure why the error, but I think I'd do it this way:

if rst.eof then
do what you need to in case of eof...., exit sub
end if

rst.MoveLast
ReDim name(1 to rst.Recordcount) As String
rst.MoveFirst
Do While Not rst.EOF
i = i + 1
name(i) = rst!L_Tech
rst.MoveNext
Loop


"eschloss" wrote:

> Access 2003
>
> I do not understand why I receive an error for the indicated line below.
> Please let me know my syntax error.
>
> Private Sub Form_Open(Cancel As Integer)
>
> Dim db As DAO.Database
> Dim rst As DAO.Recordset
> Dim sql As String
> Dim name() As String
> Dim i As Integer
> i = 0
> sql = "SELECT L_tblArea_Tech.L_Tech " & _
> "FROM L_tblArea_Tech " & _
> "WHERE L_tblArea_Tech.L_Area = 'Q/A' And L_tblArea_Tech.L_Tech_Current
> = Yes " & _
> "ORDER BY L_tblArea_Tech.L_Tech;"
> Set db = CurrentDb()
> Set rst = db.OpenRecordset(sql)
> rst.MoveFirst
>
> Do While Not rst.EOF
> i = i + 1
> ReDim name(1 To i) As String
> name(i) = rst!L_Tech <-----------Error
> here----------------
> rst.MoveNext
> Loop
>
> ReDim name(1) As String
> rst.Close
> Set rst = Nothing
>
> MsgBox name7
>
> End Sub

 
Reply With Quote
 
eschloss
Guest
Posts: n/a
 
      9th Oct 2008
Thanks for the reply Jim. When I used your suggestion, I still received the
same error on the same line. Any thoughts why?

Private Sub Form_Open(Cancel As Integer)

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Dim name() As String
Dim i As Integer

i = 0
sql = "SELECT L_tblArea_Tech.L_Tech " & _
"FROM L_tblArea_Tech " & _
"WHERE L_tblArea_Tech.L_Area = 'Q/A' And L_tblArea_Tech.L_Tech_Current
= Yes " & _
"ORDER BY L_tblArea_Tech.L_Tech;"
Set db = CurrentDb()
Set rst = db.OpenRecordset(sql)

If rst.EOF Then 'for when there are no records
End If

rst.MoveLast 'count the records beforehand
ReDim name(1 To rst.RecordCount) As String

rst.MoveFirst
Do While Not rst.EOF
i = i + 1
'ReDim name(1 To i) As String
'ReDim Preserve name(1 To i) Only if you want to keep previous info.
name(i) = rst!L_Tech
rst.MoveNext
Loop

ReDim name(1) As String
rst.Close
Set rst = Nothing

MsgBox name7

End Sub

"Jim Burke in Novi" wrote:

> Not sure why the error, but I think I'd do it this way:
>
> if rst.eof then
> do what you need to in case of eof...., exit sub
> end if
>
> rst.MoveLast
> ReDim name(1 to rst.Recordcount) As String
> rst.MoveFirst
> Do While Not rst.EOF
> i = i + 1
> name(i) = rst!L_Tech
> rst.MoveNext
> Loop
>
>
> "eschloss" wrote:
>
> > Access 2003
> >
> > I do not understand why I receive an error for the indicated line below.
> > Please let me know my syntax error.
> >
> > Private Sub Form_Open(Cancel As Integer)
> >
> > Dim db As DAO.Database
> > Dim rst As DAO.Recordset
> > Dim sql As String
> > Dim name() As String
> > Dim i As Integer
> > i = 0
> > sql = "SELECT L_tblArea_Tech.L_Tech " & _
> > "FROM L_tblArea_Tech " & _
> > "WHERE L_tblArea_Tech.L_Area = 'Q/A' And L_tblArea_Tech.L_Tech_Current
> > = Yes " & _
> > "ORDER BY L_tblArea_Tech.L_Tech;"
> > Set db = CurrentDb()
> > Set rst = db.OpenRecordset(sql)
> > rst.MoveFirst
> >
> > Do While Not rst.EOF
> > i = i + 1
> > ReDim name(1 To i) As String
> > name(i) = rst!L_Tech <-----------Error
> > here----------------
> > rst.MoveNext
> > Loop
> >
> > ReDim name(1) As String
> > rst.Close
> > Set rst = Nothing
> >
> > MsgBox name7
> >
> > End Sub

 
Reply With Quote
 
John Spencer
Guest
Posts: n/a
 
      9th Oct 2008
Also, you want to avoid using NAME as a variable. Every object in
Access has a name. So Access could be confused when you try to use name
as a variable name.

Also, if you redim a dynamic array without using the optional preserve
argument all the data is wiped from the array. Check the help for
Redim. So you would probably be better off resizing the array to the
required size ONCE as Jim Burke pointed out.

AND sizing the array once is faster then continually resizing the array.

'====================================================
John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
'====================================================


Jim Burke in Novi wrote:
> Not sure why the error, but I think I'd do it this way:
>
> if rst.eof then
> do what you need to in case of eof...., exit sub
> end if
>
> rst.MoveLast
> ReDim name(1 to rst.Recordcount) As String
> rst.MoveFirst
> Do While Not rst.EOF
> i = i + 1
> name(i) = rst!L_Tech
> rst.MoveNext
> Loop
>
>
> "eschloss" wrote:
>
>> Access 2003
>>
>> I do not understand why I receive an error for the indicated line below.
>> Please let me know my syntax error.
>>
>> Private Sub Form_Open(Cancel As Integer)
>>
>> Dim db As DAO.Database
>> Dim rst As DAO.Recordset
>> Dim sql As String
>> Dim name() As String
>> Dim i As Integer
>> i = 0
>> sql = "SELECT L_tblArea_Tech.L_Tech " & _
>> "FROM L_tblArea_Tech " & _
>> "WHERE L_tblArea_Tech.L_Area = 'Q/A' And L_tblArea_Tech.L_Tech_Current
>> = Yes " & _
>> "ORDER BY L_tblArea_Tech.L_Tech;"
>> Set db = CurrentDb()
>> Set rst = db.OpenRecordset(sql)
>> rst.MoveFirst
>>
>> Do While Not rst.EOF
>> i = i + 1
>> ReDim name(1 To i) As String
>> name(i) = rst!L_Tech <-----------Error
>> here----------------
>> rst.MoveNext
>> Loop
>>
>> ReDim name(1) As String
>> rst.Close
>> Set rst = Nothing
>>
>> MsgBox name7
>>
>> End Sub

 
Reply With Quote
 
eschloss
Guest
Posts: n/a
 
      10th Oct 2008
Thanks guys. Changing "name" fixed my issue.

"John Spencer" wrote:

> Also, you want to avoid using NAME as a variable. Every object in
> Access has a name. So Access could be confused when you try to use name
> as a variable name.
>
> Also, if you redim a dynamic array without using the optional preserve
> argument all the data is wiped from the array. Check the help for
> Redim. So you would probably be better off resizing the array to the
> required size ONCE as Jim Burke pointed out.
>
> AND sizing the array once is faster then continually resizing the array.
>
> '====================================================
> John Spencer
> Access MVP 2002-2005, 2007-2008
> The Hilltop Institute
> University of Maryland Baltimore County
> '====================================================
>
>
> Jim Burke in Novi wrote:
> > Not sure why the error, but I think I'd do it this way:
> >
> > if rst.eof then
> > do what you need to in case of eof...., exit sub
> > end if
> >
> > rst.MoveLast
> > ReDim name(1 to rst.Recordcount) As String
> > rst.MoveFirst
> > Do While Not rst.EOF
> > i = i + 1
> > name(i) = rst!L_Tech
> > rst.MoveNext
> > Loop
> >
> >
> > "eschloss" wrote:
> >
> >> Access 2003
> >>
> >> I do not understand why I receive an error for the indicated line below.
> >> Please let me know my syntax error.
> >>
> >> Private Sub Form_Open(Cancel As Integer)
> >>
> >> Dim db As DAO.Database
> >> Dim rst As DAO.Recordset
> >> Dim sql As String
> >> Dim name() As String
> >> Dim i As Integer
> >> i = 0
> >> sql = "SELECT L_tblArea_Tech.L_Tech " & _
> >> "FROM L_tblArea_Tech " & _
> >> "WHERE L_tblArea_Tech.L_Area = 'Q/A' And L_tblArea_Tech.L_Tech_Current
> >> = Yes " & _
> >> "ORDER BY L_tblArea_Tech.L_Tech;"
> >> Set db = CurrentDb()
> >> Set rst = db.OpenRecordset(sql)
> >> rst.MoveFirst
> >>
> >> Do While Not rst.EOF
> >> i = i + 1
> >> ReDim name(1 To i) As String
> >> name(i) = rst!L_Tech <-----------Error
> >> here----------------
> >> rst.MoveNext
> >> Loop
> >>
> >> ReDim name(1) As String
> >> rst.Close
> >> Set rst = Nothing
> >>
> >> MsgBox name7
> >>
> >> End Sub

>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: compile error: expected array. Bob Quintal Microsoft Access Forms 2 28th Jun 2010 12:09 AM
Compile Error:Expected Array Fatima CN Microsoft Excel Programming 1 15th Oct 2009 03:20 AM
Compile Error: Expected Array BEEJAY Microsoft Excel Programming 7 8th Dec 2008 08:23 PM
Get compile error: "expected: =" EagleOne@discussions.microsoft.com Microsoft Access Macros 2 24th May 2008 11:00 PM
compile error - expected an array =?Utf-8?B?Sm9obg==?= Microsoft Excel Programming 8 21st Sep 2006 08:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:31 PM.