Q: Copying query-result into a variable
Keith Keller; kkeller@1stnet.com wrote: I am creating a VB app that creates a Simple Query. That part works fine! What I need know is how on earth can I copy the query result into variables????? (Using VB 4.0) Example: The query returns : Fieldname = Phone FieldValue= 555-555-5555 I want to take this phone number and copy it to the variable: PhoneNum$
A: Dim SQL$ Dim rs As Recordset SQL$ = "SELECT Phone FROM " & [yourtable] Set rs = db.OpenrecordSet(SQL$) Do While Not rs.EOF MsgBox rs.Fields(0).Value rs.MoveNext Loop rs.Close Instead of showing it in a messagebox you want to save it to a variable. Because you don't know how many you get you better can use an array. Dim aPhone() As String Dim SQL$ Dim rs As Recordset dim counter% dim Max% counter% = 0 SQL$ = "SELECT Phone FROM " & [yourtable] Set rs = db.OpenrecordSet(SQL$) Do While Not rs.EOF ReDim Preserve aPhone(counter%) aPhone(counter%) = rs.Fields(0).Value counter%= counter% + 1 rs.MoveNext Loop rs.Close Max% = counter% -1 Using the variable is just calling the right array: For counter% = 0 To Max% MsgBox aPhone(counter%) Next counter% Return