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

Return value in first row of table only in VBA

I am trying to retrieve a date value from the first row of a table using VBA. I have got as far as
Expand|Select|Wrap|Line Numbers
  1. Dim RepDate as Date
  2. RepDate = DLookup("ReportDate", "tblMain")
but I can't work out how to retrieve the value contained in the first row of the table, can I use
Expand|Select|Wrap|Line Numbers
  1. DoCmd.GoToRecord acDataTable, "tblMain", acGoTo, 1
and if so how do I combine the 2?
Mar 22 '10 #1

✓ answered by patjones

Hi -

So that I can understand the problem, what's unique about the first row in the table that you always want the date value from that row? And are you sure that it will always contain the date value that you're looking for (as opposed to the fourth or ninth or 34th or whatever other row)?

DoCmd.GoToRecord is good when you have a form bound to a table, and should work if that is your situation. But it looks like you're trying to assign the date value in question to a date variable.

One thing you can try is

Expand|Select|Wrap|Line Numbers
  1. Dim rstGetFirstDate As Recordset
  2. Dim RepDate As Date
  3.  
  4. Set rstGetFirstDate = CurrentDb.OpenRecordset("tblMain")
  5. rstGetFirstDate.MoveFirst
  6.  
  7. RepDate = rstGetFirstDate!ReportDate
  8.  
  9. rstGetFirstDate.Close
  10. Set rstGetFirstDate = Nothing
  11.  

Please let me know if this is what you're looking for.

Pat

7 20944
patjones
931 Expert 512MB
Hi -

So that I can understand the problem, what's unique about the first row in the table that you always want the date value from that row? And are you sure that it will always contain the date value that you're looking for (as opposed to the fourth or ninth or 34th or whatever other row)?

DoCmd.GoToRecord is good when you have a form bound to a table, and should work if that is your situation. But it looks like you're trying to assign the date value in question to a date variable.

One thing you can try is

Expand|Select|Wrap|Line Numbers
  1. Dim rstGetFirstDate As Recordset
  2. Dim RepDate As Date
  3.  
  4. Set rstGetFirstDate = CurrentDb.OpenRecordset("tblMain")
  5. rstGetFirstDate.MoveFirst
  6.  
  7. RepDate = rstGetFirstDate!ReportDate
  8.  
  9. rstGetFirstDate.Close
  10. Set rstGetFirstDate = Nothing
  11.  

Please let me know if this is what you're looking for.

Pat
Mar 22 '10 #2
Pat

Many thanks for your help, that worked great and is useful code I will reuse.

I however realised that I was being really stupid and could just use a totalled query with Group By on ReportDate to extract the value I needed and then do a dlookup on the query, as the value in the ReportDate field is always the same for all records in the table. Apologies, I'm not used to programming in Access.
Mar 23 '10 #3
NeoPa
32,556 Expert Mod 16PB
I too, am a little confused. Your DLookup() code, missing any criteria as it is, should find the first matching record. I see no reason why that would be any other than the first record, but that rather depends on what you mean by first. RDBMSs generally treat tables as buckets of data, rather than as ordered series. records only have logical series when access via a specific index. Indexed recordsets can have order, but the order for tables is generally undefined, though an existing Primary index generally drives their access.
Mar 23 '10 #4
When I used DLookup on tblMain (RepDate = DLookup("ReportDate", "tblMain") I got the following error, "Syntax error (missing operator) in query expression "ReportDate", which is why I tried to find another way to get the RepDate value.

Thanks for your help.
Mar 23 '10 #5
patjones
931 Expert 512MB
katep -

Just for future reference, the reason you got the error is because DLookup needs a third argument. DLookup picks out one column from one row in your table on the basis of some criteria, which is supposed to be the third argument.

Pat
Mar 23 '10 #6
NeoPa
32,556 Expert Mod 16PB
I don't think that's it Pat. Criteria is an optional parameter.

I'm wondering if [reportDate] is the correct spelling of the field name?
Mar 23 '10 #7
patjones
931 Expert 512MB
Ah yes, I see that now. I don't think I've ever used DLookup in that manner. I tried it just now on a test table and it does indeed give the first record in the table.

Pat
Mar 23 '10 #8

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

Similar topics

10
by: Don | last post by:
I want the server-side php script to return a browser page that is essentially a copy of the original client page that contained the <form> which referenced the php script in the first place....
8
by: Adrian Parker | last post by:
Hi. I would like to query a database, given several where clauses to refine my search, and return the value of one single field in the database. eg: I have a table that lists teachers. Their...
30
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
6
by: michael | last post by:
Below is a simplified version of table cell mouseover script, running separate from the HTML code, that was posted on this group yesterday: var d=document, td; function mover(){...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
1
by: Anonieko | last post by:
Here are some of the approaches. 1. Transform DataGrid http://www.dotnetjohn.com/articles.aspx?articleid=36 3. Use the Export approach ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
17
by: Sara | last post by:
Hi, I'm having a problem with my program and I think it stems from me not understand how to call a function and return a int value to main. What I have to do is create a program that runs...
4
by: Mick Walker | last post by:
Hi Everyone, I am stumped here. I have the following stored proceedure:P CREATE PROCEDURE . @SupplierSKU varchar(50), @RetVal int AS Select @Retval = count(*) from dbo.ImportLines Where =...
4
by: banderson | last post by:
Hello amazing vba writers, I am trying to make a combo box return a value based on a combo box selection. I have tried a number of the codes posted here and am still having problems. I think it is...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.