Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
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.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

809 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