473,397 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Access recordset alternative?

Hi

Does anyone know an alternative way to get the result from a query in
access vba? I've got a form with a few labels that need populating.
Even if I only need a single value, the only way I can see to get some
of the data is by creating a recordset object and populating it, and
then extracting the field I need.

Here's my code:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strCompanyName As String

strSQL = "SELECT tblCompanies.Name FROM tblCompanies WHERE
CompanyID = " & Me.Recordset!CompanyID

Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

Me.lblCompanyName.Caption = rs!Name

Isn't there something like:

MyValue = RunSQL("SELECT tblCompanies.Name FROM tblCompanies WHERE
CompanyID = " & Me.Recordset!CompanyID)

Nov 13 '05 #1
1 4295
Look at DLookup in help.

Personally though I would go with what you've got. If you really have a lot
of bits like this you can always write your own function to handle it

Function MyLookup(SQL as string) as variant
Dim db As DAO.Database
Dim rs As DAO.Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset(SQL, dbOpeSnapshot)

MyLookup = rs.Fields(0).Value
End Function
and call it
dim SQL as string

SQL = "SELECT tblCompanies.Name " _
& "FROM tblCompanies " _
& " WHERE CompanyID = " _
& Me.Recordset!CompanyID

Me.lblCompanyName.Caption = MyLookUp(SQL)
or (horrible)

Me.lblCompanyName.Caption = MyLookUp("SELECT tblCompanies.Name " _
& "FROM tblCompanies "
_
& " WHERE CompanyID = "
_
&
Me.Recordset!CompanyID)

--
Terry Kreft
MVP Microsoft Access
"maccaroo" <ma******@yahoo.co.uk> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Hi

Does anyone know an alternative way to get the result from a query in
access vba? I've got a form with a few labels that need populating.
Even if I only need a single value, the only way I can see to get some
of the data is by creating a recordset object and populating it, and
then extracting the field I need.

Here's my code:

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strCompanyName As String

strSQL = "SELECT tblCompanies.Name FROM tblCompanies WHERE
CompanyID = " & Me.Recordset!CompanyID

Set db = CurrentDb
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)

Me.lblCompanyName.Caption = rs!Name

Isn't there something like:

MyValue = RunSQL("SELECT tblCompanies.Name FROM tblCompanies WHERE
CompanyID = " & Me.Recordset!CompanyID)

Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Seth Spearman | last post by:
Hey guys, I have the following code: '****************************************************** If Not Me.NewRecord Then Dim rs As DAO.Recordset Dim strBookmark As String Set rs =...
3
by: Rebecca Smith | last post by:
I’ve been using the code below in Access 97 for years now without any problems at all. Unfortunately, I now have to convert this db to Access 2000. I tried using the conversion feature and that was...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
21
by: KK | last post by:
How can I find out the maximum or minimum values out of many values in an array. I have an array in Access that loads upto 20 values depending on a sql query. I would like to find out the min and...
2
by: Jim S | last post by:
To my surprise and chagrin, newer versions of Access have disabled the functionality that lets users change the data in linked tables that point to a range in an Excel workbook. This is "because of...
2
by: super_dave_42 | last post by:
I am searching for a way to create multiple records in Access 2000 from a worksheet created in Excel. I'm pretty much self-taught with Visual Basic so forgive me if this poorly described. Basically...
0
by: jayohare | last post by:
Hello, I have code within my DB application to process credit cards through authorize.net. Ive been using the same code for several years without a problem. I have an order entry computer and...
5
by: jun | last post by:
Friends, I need to insert around 13000 records from a sybase external DB to one of my local table in access. I have the connection string set up and everything works fine except for the...
2
by: nospam | last post by:
Hello, I am using Access 2000 and SQLServer 2000. I have 2 tables - one in Access and one in SS. They are identical in structure. I want to copy the Access or local table ( table_local) to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.