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

Problem with DMax & DLookup

1,892 Expert 1GB
Howdy Errrrbody,

I'm new here! I have a few questions that I can't seem to get answered, maybe you all can help.

I am writing an access program ( first time I'm accustomed to .Net + Sql Server ) I am trying to select the Max ID from a table and store so I can use that ID in a referenced table here is what I have tried:

intReturnGoodsAuthorizationID = DMax("[intReturnGoodsAuthorizationID]", "[TReturnGoodsAuthorizations]")
This should return 10 (the highest ID) but it returns 0



intReturnGoodsAuthorizationMaxID = DLookup("[intReturnGoodsAuthorizationID]", "[MaxRGA]")
This produces a 2001 runtime error.

Here is the query, not sure what's going on in theory if the DLookup doesn't work the DMax should return something other than 0 which is not an ID in my db. This is in Access 2003.

Thanks looking forward to contributing!
AricC
Oct 20 '06 #1
12 10607
MMcCarthy
14,534 Expert Mod 8TB
You don't say where you are trying to do this. You can't do it in the default value in the table if thats what you are trying to do.

You can't return its value to itself.

If this was a query:

SELECT Max([intReturnGoodsAuthorizationID]) AS MaxID FROM[TReturnGoodsAuthorizations];

In code:

declare a local variable.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim tmpID As Long 'Assumed
  3.  
  4. tmpID = DMax("[intReturnGoodsAuthorizationID]", "[TReturnGoodsAuthorizations]") 
  5.  
  6.  
Oct 20 '06 #2
AricC
1,892 Expert 1GB
You don't say where you are trying to do this. You can't do it in the default value in the table if thats what you are trying to do.
I'm doing this in a Submit buttons click event.
You can't return its value to itself.
Jeez long Friday it's time for me to go home!

Thanks!
AricC
Oct 20 '06 #3
MMcCarthy
14,534 Expert Mod 8TB
I take it back, you can return it to a field control on your form referencing it.
Just a thought, if this is a primary key it won't let you return 10 you have to return 11.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub cmdSubmit_Click()
  3.  
  4. Me.[intReturnGoodsAuthorizationID] = DMax("[intReturnGoodsAuthorizationID]", "[TReturnGoodsAuthorizations]")+1
  5.  
  6. End Sub
  7.  
  8.  
Oct 20 '06 #4
AricC
1,892 Expert 1GB
It is the primary key that I want the max of, however it is returning the correct value... Another problem I am trying to work out is inserting all the form fields into there respective tables. I keep being asked for parameters on the variable inserted fields, also is there any way for it not to ask if you are sure you want to append 1 row? Ie.. I have to make 3 inserts then I am getting the ID's of those fields, then inserting into a child table. Here is my code: ( there may be a better way to do this like I said first MS Access app )

Thanks,
'-------------------------------------------
'btnSubmit_Click()
'-------------------------------------------
Private Sub btnSubmit_Click()

Dim dtmDateReceived As Date
Dim strCustomer As String
Dim strReturnGoodsAuthorizationNumber As String
Dim strProduct As String
Dim strSerialNumber As String
Dim strWarranty As String
Dim strCondition As String
Dim intReturnGoodsAuthorizationMaxID As Integer
Dim intSerialNumberMaxID As Integer
Dim intConditionMaxID As Integer

'Insert Return Goods Authorizations
DoCmd.RunSQL "INSERT INTO [TReturnGoodsAuthorizations](strReturnGoodsAuthorizationNumber) VALUES(txtReturnGoodsAuthorizationNumber.Value)"

'Insert Serial Number
DoCmd.RunSQL "INSERT INTO [TSerialNumbers](strSerialNumber) VALUES(txtSerialNumber.Value)"

'Insert Condition
DoCmd.RunSQL "INSERT INTO [TConditions](strCondition) VALUES(txtCondition.Value)"

'Get the Max ID's from RGA
intReturnGoodsAuthorizationMaxID = DMax("[intReturnGoodsAuthorizationID]", "[TReturnGoodsAuthorizations]")

'Get the Max ID from Serial Number
intSerialNumberMaxID = DMax("[intSerialNumberID]", "[TSerialNumbers]")

'Get the Max ID from Conditions
intConditionMaxID = DMax("[intConditionID]", "[TConditions]")

'Insert into child
DoCmd.RunSQL "INSERT INTO [TRepairs](intCustomerID, intProductID, intSerialNumberID, intReturnGoodsAuthorizationID, intWarrantyID, intConditionID, dtmDateReceived)" & _
"VALUES (cmbCustomer.Value, cmbProduct.Value, intSerialNumberMaxID, intReturnGoodsAuthorizationMaxID,cmbWarranty.Value ,intConditionMaxID,txtDateReceived.Value)"

End Sub
Oct 20 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
However, if you try to enter the maxID fields into the table it won't accept them because they are already in there. Put + 1 at the end of each of those lines if I'm right. Otherwise, try the following it should solve most of your problems.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub btnSubmit_Click()
  3. Dim intReturnGoodsAuthorizationMaxID As Integer
  4. Dim intSerialNumberMaxID As Integer
  5. Dim intConditionMaxID As Integer
  6.  
  7. 'turn off access add record warning 
  8. DoCmd.SetWarnings False
  9.  
  10. 'Insert Return Goods Authorizations
  11. DoCmd.RunSQL "INSERT INTO [TReturnGoodsAuthorizations](strReturnGoodsAuthorizationNumber) VALUES('" & Me.txtReturnGoodsAuthorizationNumber & "');"
  12.  
  13. 'Insert Serial Number
  14. DoCmd.RunSQL "INSERT INTO [TSerialNumbers] (strSerialNumber) VALUES('" & Me.txtSerialNumber.Value & "');"
  15.  
  16. 'Insert Condition
  17. DoCmd.RunSQL "INSERT INTO [TConditions] (strCondition) VALUES('" & txtCondition & "');"
  18.  
  19. 'Get the Max ID's from RGA
  20. intReturnGoodsAuthorizationMaxID = DMax("[intReturnGoodsAuthorizationID]", "[TReturnGoodsAuthorizations]")
  21.  
  22. 'Get the Max ID from Serial Number
  23. intSerialNumberMaxID = DMax("[intSerialNumberID]", "[TSerialNumbers]")
  24.  
  25. 'Get the Max ID from Conditions
  26. intConditionMaxID = DMax("[intConditionID]", "[TConditions]")
  27.  
  28. 'Insert into child
  29. DoCmd.RunSQL "INSERT INTO [TRepairs] (intCustomerID, intProductID, intSerialNumberID, intReturnGoodsAuthorizationID, intWarrantyID, intConditionID, dtmDateReceived) " & _
  30. "VALUES ('" & cmbCustomer & "','" & cmbProduct & "'," & intSerialNumberMaxID & "," & intReturnGoodsAuthorizationMaxID & ",'" & cmbWarranty & "'," & intConditionMaxID & ",#" & txtDateReceived & "#);"
  31.  
  32. DoCmd.SetWarnings True
  33.  
  34. End Sub
  35.  
  36.  
Oct 20 '06 #6
AricC
1,892 Expert 1GB
MMcarthy,
Let me ask you what did you include the # sign in the last 2 Values for?

I get a syntax error(missing operator) on this line:

DoCmd.RunSQL "INSERT INTO [TConditions] (strCondition) VALUES('" & Me.txtCondition.Value & "');"

The syntax looks the exact same as the other fields.
Oct 20 '06 #7
MMcCarthy
14,534 Expert Mod 8TB
MMcarthy,
Let me ask you what did you include the # sign in the last 2 Values for?
# on either side of a date field. Not always required but good practice.



DoCmd.RunSQL "INSERT INTO [TConditions] (strCondition) VALUES ('" & Me.txtCondition & "');"

I just put in a space after VALUES and removed .Value. See if it makes any difference
Oct 20 '06 #8
AricC
1,892 Expert 1GB
Is there any easy way built into access to check for a duplicate entry and only allow one without setting index to no duplicates?
Oct 21 '06 #9
AricC
1,892 Expert 1GB
Also,
Does anyone know of a refresher once I enter a record it stays kind of cached how to I refresh and get a fresh blank copy of the form?

Thanks!
Aric
Oct 21 '06 #10
MMcCarthy
14,534 Expert Mod 8TB
AricC

I don't understand what you're asking in your last two posts.

If these are separate issues can you post them separately and with a more detailed explanation.
Oct 21 '06 #11
AricC
1,892 Expert 1GB
AricC
I don't understand what you're asking in your last two posts.
If these are separate issues can you post them separately and with a more detailed explanation.
Ok once you enter a record and hit submit. The info is inserted into Tables, is there any way to make the record 'Refresh' or should I programmatically set the fields to not have anything in the text/combo boxes?
Aric
Oct 23 '06 #12
AricC
1,892 Expert 1GB
Ok once you enter a record and hit submit. The info is inserted into Tables, is there any way to make the record 'Refresh' or should I programmatically set the fields to not have anything in the text/combo boxes?
Aric
Disregard I took care of it!
Oct 23 '06 #13

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

Similar topics

0
by: Ronan | last post by:
Hi, I have a problem with a form using the PHP PEAR HTML_QuickForm package & javascript: I want to record the content of my form into a mySQL database then execute a javascript function. ...
2
by: JoRo | last post by:
Hi, I'm trying to add an onclick event to a button control so it will open up a new popup window. Everything works fine except where I add variables to pass on to the popup window. Where I have...
2
by: P. Mellerin | last post by:
Hello, I have been searching online for a whole day and couldn't find an answer for this. I have this hyperlink server control: ...
1
by: Nathan Alden | last post by:
I have an XSD defined as the following: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"...
2
by: Amit | last post by:
I have assigned a simple xml into a XmlReader. A switch case loops through the nodes and writes the data to XmlWriter object which is initialized to a xml file. The output xml file is correctly...
0
by: Amitabh Saikia | last post by:
Hi I have created a simple function called 'addTheString(string& str, string appstr)' and the intension is to append the 'appstr' to 'str' and use 'str'. I tried swigging this method. My...
4
by: spectecjr | last post by:
Hey folks; I've got a weird problem... whenever I try to drag and drop a toolbox item onto a form in the Window Forms editor, the control is automatically added on the first click. This gives...
6
by: avcitamer | last post by:
We upgraded our system and problem below occured, pleas help me... Windows 2003 server SP1 When I set a decimal DB field value to "123,32" (using ADODB.recordset ) updated value was ok (123.32)...
3
AMT India
by: AMT India | last post by:
I am using POST method in Ajax. I have to send a number of items through POST. So I am creating a variable like this : params += "name="+document.getElementById(name').value+"&"; params...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.