By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,460 Members | 2,215 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,460 IT Pros & Developers. It's quick & easy.

Getting data from SQL Select Via VBA

P: 10
I currently have no Problems with the following code;

1. Dim MyRec As DAO.Recordset
2. Set db = CurrentDb
3. Set MyRec = db.OpenRecordset("SELECT * FROM Table")
4.
5. Text1.Value = MyRec![Field1]

This works fine when the field name on line 5 is written manually in the code. I am however trying to make this a variable. However when i do i get the error

"Item not found in this selection"

Now i don't know whether there is any missing syntax or i need to change the data type of the variable to something other than a string. Any help would be great guys.

Cheers,

T
Dec 27 '07 #1
Share this Question
Share on Google+
2 Replies


Denburt
Expert 100+
P: 1,356
I currently have no Problems with the following code;

1. Dim MyRec As DAO.Recordset
2. Set db = CurrentDb
3. Set MyRec = db.OpenRecordset("SELECT * FROM Table")
4.
5. Text1.Value = MyRec![Field1]

This works fine when the field name on line 5 is written manually in the code. I am however trying to make this a variable. However when i do i get the error

"Item not found in this selection"

Now i don't know whether there is any missing syntax or i need to change the data type of the variable to something other than a string. Any help would be great guys.

Cheers,

T
Try the following, hope this helps. Let us know.

Dim MyRec As DAO.Recordset
Dim myVar
Set db = CurrentDb
Set MyRec = db.OpenRecordset("SELECT * FROM Table")

Me!Text1.Value = MyRec(myVar)
Dec 27 '07 #2

P: 10
Cheers,

I actually ended up using

MyRec.Fields(fieldName)

Thanks for the help
Dec 27 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.