473,378 Members | 1,321 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,378 software developers and data experts.

Looking up value in code from a SQL statement

Hi everyone
Normally, if I need to look up a value in a Code, from a query, I
write the VBA code stated below. I want to find out if there is an
alternate way to lookup because this is a lot of code to find one
value.

I have also used dLookup function which does lookup a vaule from a
query, however, I am not sure if you can lookup a value from a sql
statement and NOT from a saved query.

Do we have any alternatives to dLookup?
************************************************** ************************************************** **
dim sSQL as string
dim rst as recordset

sSQL = "Select * from tblCustomers where numCustomerID = " &
frm.numCustomerID
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)

if not rst.EOF then
CustomerName = rst!txtCustomerName
End If
rst.Close
Set rst = Nothing
************************************************** ************************************************** **
Oct 21 '08 #1
7 1335
On Oct 21, 7:00*pm, Niranjan <nhi...@gmail.comwrote:
Hi everyone
Normally, if I need to look up a value in a Code, from a query, I
write the VBA code stated below. I want to find out if there is an
alternate way to lookup because this is a lot of code to find one
value.

I have also used dLookup function which does lookup a vaule from a
query, however, I am not sure if you can lookup a value from a sql
statement and NOT from a saved query.

Do we have any alternatives to dLookup?
************************************************** ************************* ***************************
dim sSQL as string
dim rst as recordset

sSQL = "Select * from tblCustomers where numCustomerID = " &
frm.numCustomerID
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)

if not rst.EOF then
* * CustomerName = rst!txtCustomerName
End If
rst.Close
Set rst = Nothing
************************************************** ************************* ***************************
Is it not sufficient to write?:
CustomerName = dlookup("CustomerName","tblCustomers","numCustomer ID =
" & frm.numCustomerID)
Oct 21 '08 #2
On Oct 21, 11:06*am, Jim Devenish <internet.shopp...@foobox.com>
wrote:
On Oct 21, 7:00*pm, Niranjan <nhi...@gmail.comwrote:


Hi everyone
Normally, if I need to look up a value in a Code, from a query, I
write the VBA code stated below. I want to find out if there is an
alternate way to lookup because this is a lot of code to find one
value.
I have also used dLookup function which does lookup a vaule from a
query, however, I am not sure if you can lookup a value from a sql
statement and NOT from a saved query.
Do we have any alternatives to dLookup?
************************************************** ************************* ***************************
dim sSQL as string
dim rst as recordset
sSQL = "Select * from tblCustomers where numCustomerID = " &
frm.numCustomerID
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
if not rst.EOF then
* * CustomerName = rst!txtCustomerName
End If
rst.Close
Set rst = Nothing
************************************************** ************************* ***************************

Is it not sufficient to write?:
CustomerName = dlookup("CustomerName","tblCustomers","numCustomer ID =
" & frm.numCustomerID)- Hide quoted text -

- Show quoted text -
In this case, it is because the query is based upon only one table.
However, if the query is based on multiply tables, I dont think I can
use the sql statement in place of "tblCustomers" in the dlookup
function. I have never tried it, may be it will work. Does anyone know?
Oct 21 '08 #3
On Oct 21, 7:06*pm, Jim Devenish <internet.shopp...@foobox.comwrote:
On Oct 21, 7:00*pm, Niranjan <nhi...@gmail.comwrote:
Hi everyone
Normally, if I need to look up a value in a Code, from a query, I
write the VBA code stated below. I want to find out if there is an
alternate way to lookup because this is a lot of code to find one
value.
I have also used dLookup function which does lookup a vaule from a
query, however, I am not sure if you can lookup a value from a sql
statement and NOT from a saved query.
Do we have any alternatives to dLookup?
************************************************** ************************* ***************************
dim sSQL as string
dim rst as recordset
sSQL = "Select * from tblCustomers where numCustomerID = " &
frm.numCustomerID
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
if not rst.EOF then
* * CustomerName = rst!txtCustomerName
End If
rst.Close
Set rst = Nothing
************************************************** ************************* ***************************

Is it not sufficient to write?:
CustomerName = dlookup("CustomerName","tblCustomers","numCustomer ID =
" & frm.numCustomerID)
The help system says that you can use a table name or a query name.
So not an SQL statement.
Oct 21 '08 #4
Dim CustomerName$
On Error Resume Next
CustomerName = DBEngine(0)(0).OpenRecordset( _
"Select * from tblCustomers where numCustomerID = " _
& frm.numCustomerID)(0)
On Error GoTo 0

On Oct 21, 2:00*pm, Niranjan <nhi...@gmail.comwrote:
Hi everyone
Normally, if I need to look up a value in a Code, from a query, I
write the VBA code stated below. I want to find out if there is an
alternate way to lookup because this is a lot of code to find one
value.

I have also used dLookup function which does lookup a vaule from a
query, however, I am not sure if you can lookup a value from a sql
statement and NOT from a saved query.

Do we have any alternatives to dLookup?
************************************************** ************************* ***************************
dim sSQL as string
dim rst as recordset

sSQL = "Select * from tblCustomers where numCustomerID = " &
frm.numCustomerID
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)

if not rst.EOF then
* * CustomerName = rst!txtCustomerName
End If
rst.Close
Set rst = Nothing
************************************************** ************************* ***************************
Oct 21 '08 #5
On Oct 21, 11:31*am, lyle fairfield <lyle.fairfi...@gmail.comwrote:
Dim CustomerName$
On Error Resume Next
CustomerName = DBEngine(0)(0).OpenRecordset( _
"Select * from tblCustomers where numCustomerID = " _
& frm.numCustomerID)(0)
On Error GoTo 0

On Oct 21, 2:00*pm, Niranjan <nhi...@gmail.comwrote:
Hi everyone
Normally, if I need to look up a value in aCode, from aquery, I
write the VBAcodestated below. I want to find out if there is an
alternate way to lookup because this is a lot ofcodeto find one
value.
I have also used dLookup function which does lookup a vaule from a
query, however, I am not sure if you can lookup a value from a sql
statement and NOT from a savedquery.
Do we have any alternatives to dLookup?
************************************************** ************************* ***************************
dim sSQL as string
dim rst as recordset
sSQL = "Select * from tblCustomers where numCustomerID = " &
frm.numCustomerID
Set rst = CurrentDb.OpenRecordset(sSQL, dbOpenSnapshot)
if not rst.EOF then
* * CustomerName = rst!txtCustomerName
End If
rst.Close
Set rst = Nothing
************************************************** ************************* ***************************- Hide quoted text -

- Show quoted text -
I am getting a Run-Time Error 13 "Type Mismatch" with the code above.
Any ideas?
Oct 21 '08 #6
Niranjan <nh****@gmail.comwrote in
news:ca**********************************@w39g2000 prb.googlegroups.com:
On Oct 21, 11:31*am, lyle fairfield <lyle.fairfi...@gmail.comwrote:
>Dim CustomerName$
On Error Resume Next
CustomerName = DBEngine(0)(0).OpenRecordset( _
"Select * from tblCustomers where numCustomerID = " _
& frm.numCustomerID)(0)
On Error GoTo 0
I am getting a Run-Time Error 13 "Type Mismatch" with the code above.
Any ideas?
1. That you should click on debug and find the line where the error
occurs?

2. That I didn't copy the code exactly to my post?

3. That you didn't copy the code exactly?

4. It's very hard to get a runtime error after On Error Resume Next.

If I had to bet I'd bet on 1, 3 and 4. But it's not as sure as Curlin.

This works in Northwind 2007. You could modify it for your own needs.

Function CustomerName$()
On Error Resume Next
CustomerName = DBEngine(0)(0).OpenRecordset( _
"Select * from Customers where ID = 3")(0)
On Error GoTo 0
End Function

Sub temp()
Debug.Print CustomerName
End Sub

Perhaps you think real programmers don't use "On Error Resume Next". You
and a few million others are wrong. People write their own error handling
because it reenforces their self-image as sophisticated programmers. The
fact that 99.44% of error handling routines are redundant doesn't bother
them at all if writing them makes them king of the hill (even if it's a
tiny hill.)

BTW, the "On Error Resume Next" line here simply effects an empty string
being returned if the SQL call fails, or there are no records.

--
lyle fairfield
Oct 22 '08 #7
"Niranjan" <nh****@gmail.comwrote in message
news:a9**********************************@a18g2000 pra.googlegroups.com...
On Oct 21, 11:06 am, Jim Devenish <internet.shopp...@foobox.com>
wrote:
In this case, it is because the query is based upon only one table.
However, if the query is based on multiply tables, I dont think I can
use the sql statement in place of "tblCustomers" in the dlookup
function. I have never tried it, may be it will work. Does anyone know?
So why not use DLookup on a stored query on all required tables?

Keith.

Oct 22 '08 #8

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

Similar topics

4
by: dmiller23462 | last post by:
So here's my problem.....I need to set up different email distributions based on which option in the following Select form has been chosen....For instance if "Putaway" is chosen it needs to email...
4
by: Mick White | last post by:
mysql> select * from guestbook; +----+--------+---------+-----------------+----------------+ | id | fname | lname | comments | time_in |...
9
by: ckerns | last post by:
I want to loop thru an array of controls,(39 of them...defaults = 0). If value is null or non-numeric I want to assign the value of "0". rowString = "L411" //conrol name if (isNaN(eval...
3
by: Donna Sabol | last post by:
I know a little about VBA, but everything I know I've learned on my own through trial and error (and cut & paste). Now I'm actually taking a class, but it's online twice a week. I'm running...
6
by: lovecreatesbeauty | last post by:
I ever missed a `return' statement when write a function `int HighDigit(Num)' to get the highest digit of an integer. But even if the `return' statement is ignored the function still can obtain...
1
by: Randy Volkart | last post by:
I'm trying to fix a glitch in a complex access database, and have a fairly complex problem... unless there's some obscure easy fix I don't know being fairly new with Access. Basically, the area...
14
by: Rex | last post by:
Re: Looking for Tips/Writeup on overall approach to Exception Processing Hi All - I am fairly new to C# and am wondering how to best implement (overall) Exception Processing within my...
5
by: xirowei | last post by:
public class Result { private int countA = 0; private int countB = 0; private int statement; private boolean statusA = false; private boolean statusB = false; private int arrayA = new...
20
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...

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.