4

My VBA is quite rusty and I seem to be drawing a blank. I am using MS Access 2010 if it matters.

I have the results of a query that return two fields (Field1 is text and Field2 is double) and five records and I want to assign Field2 values to five different variables based on what is in Field1. To me this is some kind of case statement - how would I do this in VBA on the recordset ?

  Private Sub test()

  Dim myRS As DAO.Recordset
  Dim db As Database
  Dim strSQL As String
  Dim v1, v2, v3, v4, v5 As Double

  ' Pretend strSQL is a different query that gives five records

  Set dbs = CurrentDb
  strSQL = "SELECT Field1, BigNumber FROM tmp1"
  Set myRS = dbs.OpenRecordset(strSQL)

  Do While Not myRS.EOF
      v1 = ?         ' I want v1 = Field2 when Field1="A"
      v2 = ?         ' I want v2 = Field2 when Field1="B"
      v3 = ?         ' I want v3 = Field2 when Field1="C"
      v4 = ?         ' I want v4 = Field2 when Field1="D"
      v5 = ?         ' I want v5 = Field2 when Field1="E"
  Loop

  End Sub

Many Thanks !

1
  • 1
    What are you doing with the variables v1-v5? I'm thinking I have a better solution than looping through the dataset, but I'd need to know what the end game is to know for sure. Commented Feb 20, 2013 at 16:48

3 Answers 3

5
Private Sub test()

Dim myRS As DAO!Recordset
Dim db As Database
Dim strSQL As String
Dim v1, v2, v3, v4, v5 As Double

' Pretend strSQL is a different query that gives five records

Set dbs = CurrentDb
strSQL = "SELECT Field1, BigNumber FROM tmp1"
Set myRS = dbs!OpenRecordset(strSQL)

Do While Not myRS!EOF
    Select Case myRS.Fields(0)
    Case "A"
        v1 = myRS.Fields(1)
    Case "B"
        v2 = myRS.Fields(1)
    Case "C"
        v3 = myRS.Fields(1)
    Case "D"
        v4 = myRS.Fields(1)
    Case "E"
        v5 = myRS.Fields(1)
    End Select
Loop
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I get an error that Field1 is a Method or data member that cannot be found.
I have to remove the ! from the code is that a problem ? It still gives me the same error.
1

I think you are missing MoveNext:

Do While Not rec.EOF
    rec.MoveNext
Loop

Comments

0

In this case, you may as well just use DlookUp and forget about the recordset:

 v1 = DLookUp("BigNumber","tmp1","Field1='A'")
 v2 = DLookUp("BigNumber","tmp1","Field1='B'")

 <...>

4 Comments

Well, the problem is that my SQL statement is much more complicated than pulling data from one table, I was just making a simplification for the question.
If your sql is more complicated, it should be possible to build it to return 5 columns, or to return the data in an unambiguous way. Nearly anything is better than walking a recordset.
If I return five columns then I still need to assign the results to five variables, or I just make 5 queries and assign the results to variables - it is a lot more typing that is for sure.
If you return 5 columns, you are not walking a recordset, v1=col1, v2=col2, a lot less typing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.