Solved

Retrieving a key field using a variable

Posted on 2004-05-01
7
167 Views
Last Modified: 2010-04-24
I am now able to insert a variable into my Table. Now I want to take this same variable and extract the key field value for this row.

This is an Access Table. The table name is tblAGMain. The fields in the table are: KeyAG (Autonumber field and keyed), dtDate (Date field type), dtTime (Text field type), Locator (Text field type), Initials (Text field type).

Now this is the bit of code that I am trying to use to retrieve the key field value, bu I always get a value of "0".

Dim cmdDate As OleDbCommand = cnAGPOUpDate.CreateCommand()
cmdDate.CommandText = "INSERT INTO tblAGMain (dtDate, tmTime, Initials, Locator) VALUES ('" & strToday & "', " & _
"Null, '" & strLocator & "', Null)"
cmdDate.ExecuteNonQuery()

'create a new command to retrieve the key field from the tblAGMain where the date was just inserted
Dim cmdAGKey As OleDbCommand = cnAGPOUpDate.CreateCommand()
cmdAGKey.CommandText = "SELECT KeyAg FROM tblAGMain WHERE Locator = '" & strLocator & "'"
intAGKey = CInt(cmdAGKey.ExecuteScalar())

strAGKey = intAGKey.ToString

MessageBox.Show(strAGKey)

What is wrong with my code, why am I not getting my key field valu back?

Thanks
0
Comment
Question by:cmem
  • 3
  • 2
7 Comments
 

Author Comment

by:cmem
ID: 10968524
Does anyone have any ideas?
0
 
LVL 27

Accepted Solution

by:
Dabas earned 25 total points
ID: 10969248
Hi cmem:
> KeyAG (Autonumber field and keyed)
1) What do you mean by Autonumber and keyed? Its either Autonumber or not.
2) Does the insert command actually create the new record? (Have you checked the table in Access?)

Dabas
0
 

Author Comment

by:cmem
ID: 10969609
It is an autonumber field and it is the key field for the table. But I see what happened, the fields on the table got rearranged and my app was inserting the variable into the wrong field and when it went to find the value it of course was not in that field.
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10969626
cmem:
I assume then that you do not have a problem any more?

Dabas
0
 

Author Comment

by:cmem
ID: 10969888
No that resolved that issue thanks....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

23 Experts available now in Live!

Get 1:1 Help Now