Solved

Retrieving a key field using a variable

Posted on 2004-05-01
7
170 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
[X]
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
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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