Solved

VBA Insert result from sql statement into a form of a field

Posted on 2014-10-17
5
465 Views
Last Modified: 2014-10-17
Hi,
I was hoping to get some help with the following code. I have a field that I need to populate with the result of a sql select statement. Cant seem to work

So when I click a button I need the field to be populated with the max of a value

Me.provid = DoCmd.RunSQL "select max(id) from ser_faxes_file"

Open in new window


somehow it cant seem to work. I was reading a bit and noticed that docmd.runsql is not used for select statements. Not sure what to use.

thanks,
0
Comment
Question by:damixa
  • 3
  • 2
5 Comments
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 40386765
What you want is this:

Me.provid = DMax("[id]","ser_faxes_file")

 This is using a Domain function, of which there are several (DMax(), Dlookup(), etc).   All encapsulate a SQL statement and are intended to be used where SQL statements are not directly.

Your other option when working in code is to open a recordset, read the record(s) returned, and grab your values.

DoCmd.RunSQL

 is a statement used by itself to execute "action queries"; those that don't return records (such as appends, make table, updates, etc) but only execute.

 and instead of that, you're better off with using:

CurrentDB().Execute   <SQL statement>, dbFailOnError

 Because with the last argument, dbFailOnError, you can add error trapping and tell if something goes wrong.  You can't do that with RunSQL.

Jim.
0
 

Author Comment

by:damixa
ID: 40386810
HI Jim,
That Me.provid = DMax("[id]","ser_faxes_file") Worked perfect.
I'm a newbie, but if I wanted to add 1 to the max, what would the syntax be?

thanks,
Vinnie
0
 

Author Comment

by:damixa
ID: 40386816
I tried the Me.provid = DMax("[id]","ser_faxes_file")  +1 but got a type mismatch
0
 

Author Comment

by:damixa
ID: 40386821
Actually, I got it to work. Thanks a ton.
0
 
LVL 57
ID: 40386831
<<I tried the Me.provid = DMax("[id]","ser_faxes_file")  +1>>

If your doing this to generate a sequential number, be aware that DMax() can fail in a multi-user situation (it's possible for two users to end up with the same value.

Instead, you'd use a key generation procedure which locks a table allowing only one value to be generated at a time.

Jim.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now