vba store recordset as integer variable

Sean Colter O'Connell

First time poster, I finally had a question that I couldn't find an answer to here.

I have an MS Access query that returns 1 result (which is a number) that I want to store as an integer variable (x) so I can use it later for a loop. The issue is that because I'm using it as a recordset and the variable is an integer, I'm receiving a "Type Mismatch" error.

Right now I'm just storing the result to a cell and setting the variable equal to the cell:

Ws.Range("A1") = Db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").GetRows(1)

x = Ws.Range("A1")

Ws.Range("A1").Delete

And then later I just have a loop that runs x times:

For i = 0 To x

Basically, I just want to have some code that looks like this:

x = Db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").GetRows(1)

Any help here would be huge. Thank you!

Ralph

The following should give you the correct result:

Dim x As Integer
Dim db As DAO.Recordset

db.MoveFirst
If IsNumeric(db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").Fields(0).Value) Then
    x = CInt(db.OpenRecordset("SELECT COUNT(Asset_Name) FROM Assets WHERE Active = True").Fields(0).Value)
Else
    MsgBox "The query did not return a number." & Chr(10) & "Aborting..."
End If

Note, that you are using DAO and not ADO as your original tags on the post indicated. Still, they both behave rather similar and the cursor is normally on the first row (when the data is returned). So, MoveFirst should not be necessary. Still, Microsoft themselves keep using it in its own sample code all the time. The first column if for DAO and ADO alike .Fields(0).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

recordset.find using variable VBA ADODB

From Dev

VBA store msoThemeColor in variable

From Dev

How could an integer variable store a reference to integer?

From Dev

Store into variable result of function VBA

From Java

Storing Recordset to Array in VBA

From Dev

Access VBA Recordset

From Dev

Merge mysql recordset in vba

From Dev

Passing A Parameter for a VBA Recordset

From Dev

Is it possible to Store 0 as the first digit in integer variable

From Dev

how is it possible to store an integer in a character variable?

From Dev

Is it possible to Store 0 as the first digit in integer variable

From Dev

Store location of cell address to variable in VBA

From Dev

How to Store Variable in Excel Application Memory VBA

From Dev

VBA: Retrieve data from a filter and store it in a variable

From Dev

How to get the rows of a RecordSet in VBA

From Dev

Excel VBA - Loop through recordset

From Dev

Access VBA - Error on checking recordset

From Dev

Add and Paste Recordset object VBA

From Dev

Conditional looping with ADO Recordset in VBA

From Dev

excel/VBA how to assign the variable length of an array to integer variable

From Dev

How does Java store Negative numbers in an Integer Variable?

From Dev

How do I store a random integer in a local variable in Python?

From Dev

How do you store integer into a variable and computeAvg in java?

From Dev

How to retrieve integer data from database and store in variable?

From Dev

Error setting VBA Recordset of form from ADODB.recordset

From Dev

vba loop through fields in recordset while another recordset is not EOF

From Dev

vba loop through fields in recordset while another recordset is not EOF

From Dev

Error setting VBA Recordset of form from ADODB.recordset

From Dev

Fetch Data from recordset and store it in list

Related Related

  1. 1

    recordset.find using variable VBA ADODB

  2. 2

    VBA store msoThemeColor in variable

  3. 3

    How could an integer variable store a reference to integer?

  4. 4

    Store into variable result of function VBA

  5. 5

    Storing Recordset to Array in VBA

  6. 6

    Access VBA Recordset

  7. 7

    Merge mysql recordset in vba

  8. 8

    Passing A Parameter for a VBA Recordset

  9. 9

    Is it possible to Store 0 as the first digit in integer variable

  10. 10

    how is it possible to store an integer in a character variable?

  11. 11

    Is it possible to Store 0 as the first digit in integer variable

  12. 12

    Store location of cell address to variable in VBA

  13. 13

    How to Store Variable in Excel Application Memory VBA

  14. 14

    VBA: Retrieve data from a filter and store it in a variable

  15. 15

    How to get the rows of a RecordSet in VBA

  16. 16

    Excel VBA - Loop through recordset

  17. 17

    Access VBA - Error on checking recordset

  18. 18

    Add and Paste Recordset object VBA

  19. 19

    Conditional looping with ADO Recordset in VBA

  20. 20

    excel/VBA how to assign the variable length of an array to integer variable

  21. 21

    How does Java store Negative numbers in an Integer Variable?

  22. 22

    How do I store a random integer in a local variable in Python?

  23. 23

    How do you store integer into a variable and computeAvg in java?

  24. 24

    How to retrieve integer data from database and store in variable?

  25. 25

    Error setting VBA Recordset of form from ADODB.recordset

  26. 26

    vba loop through fields in recordset while another recordset is not EOF

  27. 27

    vba loop through fields in recordset while another recordset is not EOF

  28. 28

    Error setting VBA Recordset of form from ADODB.recordset

  29. 29

    Fetch Data from recordset and store it in list

HotTag

Archive