?
Solved

Retrieving a key field using a variable

Posted on 2004-05-01
7
Medium Priority
?
174 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
5 Comments
 

Author Comment

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

Accepted Solution

by:
Dabas earned 100 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

839 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