Solved

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

Posted on 2014-10-17
5
462 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

21 Experts available now in Live!

Get 1:1 Help Now