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

Retrieving a key field using a variable

Posted on 2004-05-01
7
169 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

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