473,378 Members | 1,456 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.

How can I store SQL result into variable?

18
I need to know how can I store SQL result into variable, I have used the below code but it dose not work.

Is there an easier way to do this?

---------------------------------------------------------
Dim dbsCurrent As Database
Dim count As Recordset
Dim result As String

Set dbsCurrent = CurrentDb
Set count = dbsCurrent.OpenRecordset("SELECT Estate.ElectricMeterNumber FROM Estate WHERE ((Estate.EstateNumber)= 1)")

[ElectricMeterNumber] = result
count.Close
dbsCurrent.Close
Sep 14 '07 #1
6 43685
Scott Price
1,384 Expert 1GB
Will you have more than one meter number for each estate?

If so you will need to store the result into an array.

This is an example code pulled from my test db, it's not intended to solve your particular problem, just provide you with one possible way to use recordsets and arrays to capture values...

Expand|Select|Wrap|Line Numbers
  1. Public Function EventCustomer() As String
  2.  
  3. Dim MyArray() As Variant
  4. Dim evcust As String
  5. Dim cust As String
  6. Dim ev As String
  7. Dim rs As DAO.Recordset
  8. Dim ls As Integer
  9. Dim intCounter As Integer
  10. Dim list As Integer
  11. DoCmd.OpenForm "frmEventCustomer", , , , , acHidden
  12.  
  13. Set rs = Forms!frmEventCustomer!lstEventCustomer.Recordset
  14. Erase MyArray
  15.             With rs
  16.                 .MoveFirst
  17.                 .MoveLast
  18.                 ls = .RecordCount
  19.                 .MoveFirst
  20.                 MyArray() = .GetRows(ls)
  21.             End With
  22.  
  23. ev = MyArray(1, 0)
  24. cust = MyArray(2, 0)
  25. list = 0
  26. evcust = ev & ": " & cust
  27. For intCounter = 1 To (ls - 1)
  28.         list = list + 1
  29.         cust = MyArray(2, list)
  30.         evcust = evcust & ", " & cust
  31. Next
  32. EventCustomer = evcust
  33. DoCmd.Close acForm, "frmEventCustomer", acSaveNo
  34. Erase MyArray
  35. rs.Close
  36.  
  37. End Function
Regards,
Scott
Sep 14 '07 #2
JConsulting
603 Expert 512MB
I need to know how can I store SQL result into variable, I have used the below code but it dose not work.

Is there an easier way to do this?

---------------------------------------------------------
Dim dbsCurrent As Database
Dim count As Recordset
Dim result As String

Set dbsCurrent = CurrentDb
Set count = dbsCurrent.OpenRecordset("SELECT Estate.ElectricMeterNumber FROM Estate WHERE ((Estate.EstateNumber)= 1)")

[ElectricMeterNumber] = result
count.Close
dbsCurrent.Close
if it's just a one time deal

Dim Myval
myval = dlookup("myfield","mytable","Somefield=somevalue")

that will store the value of "myfield" into the variable myval

Hope this helps
J
Sep 15 '07 #3
Busbait
18
Thanks Scott and JConsulting for your replies.

I would appreciate if you can provide me with an example of How to store SQL result into variable assuming that the SQL statement will return only single value.
Sep 15 '07 #4
Scott Price
1,384 Expert 1GB
Thanks Scott and JConsulting for your replies.

I would appreciate if you can provide me with an example of How to store SQL result into variable assuming that the SQL statement will return only single value.
Like J said try this:

Dim MyVal As Integer

MyVal = DLookup("ElectricMeterNumber", "Estate", "[EstateNumber] = 1")

(This is assuming that your ElectricMeterNumber is an integer value... if it could use decimal points, change the variable declaration to Double)

Regards,
Scott
Sep 15 '07 #5
JConsulting
603 Expert 512MB
Thanks Scott and JConsulting for your replies.

I would appreciate if you can provide me with an example of How to store SQL result into variable assuming that the SQL statement will return only single value.

You had originally asked if there was an easier way...thta's what the Dlookup() function was...easier.
This works the way you originally designed it I suppose.
J

Expand|Select|Wrap|Line Numbers
  1. Dim dbsCurrent As Database
  2. Dim rsCount As Recordset
  3. Dim result As String
  4.  
  5. Set dbsCurrent = CurrentDb
  6. Set rsCount = dbsCurrent.OpenRecordset("SELECT Estate.ElectricMeterNumber FROM Estate WHERE ((Estate.EstateNumber)= 1)")
  7.  
  8. result = rsCount![ElectricMeterNumber]
  9. rsCount.Close
  10.  
Sep 15 '07 #6
Busbait
18
Thanks Gentlemen for your support
Sep 15 '07 #7

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

Similar topics

1
by: Adrian | last post by:
Sorry if this appears twice but 6 hours and wasn't up there! "Adrian" <Adrian@nospamhotmail.com.uk> wrote in message news:... > Hi > Is it possible and if so how to do the following? > > I...
12
by: harishg2 | last post by:
Hi, How to store a variable value for more than one executions. Ex: main() { int i=0; i++; printf("%d",i);
9
by: VMI | last post by:
I have two tables and I want to compare these two tables with a query( ie. "select * from A where B.key = A.key") and the result will be stored in a 3rd table (table C). is this possible? If...
2
by: Abdul Qadir Khan | last post by:
Hi All, Is it possible to call a function whose name is store in variable using VB.Net. Assume this dim a as string a = "GetEmployee()" Now I have function name in a variable, how can I...
1
by: Joey Liang via DotNetMonster.com | last post by:
Hi all, Is it possible to store double variable to session? If possible can show me code sample on how to convert.Thanx n advance. -- Message posted via http://www.dotnetmonster.com
3
by: seralasu | last post by:
Hi, I have a problem. When The page refresh, I don't store variable value. I'm using SESSION variables but a lot of transition then I don't control SESSION variable. I may use Temprorary Table but...
1
by: ovisvana | last post by:
Hi, Iam using javascript to submit a form to a php page on a remote server which is returning me another php page. What I would like is to store the contents of the php page in a variable in my...
3
by: KritiGuleria | last post by:
i need to put the result of quey @st into a variable. could anyone please tell me how to do that? select @sys= + columnName+', ' from tab set @sys=left(@sys, len(@sys)-1) select @sys ...
4
by: wish | last post by:
Hi all, Where is the location for store session variable? Because i feel that the session variable is keep store the previous variable and no release the variable. When i click back button,...
1
by: sana krishna | last post by:
How can we take the value of a variable in javascript function to the ASP.NET string I wish to display the value of P using Response.write when I click the Button. //my code <html> <form>...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.