Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can not write values to Db in Access form.

Posted on 2016-08-02
4
Medium Priority
?
76 Views
Last Modified: 2016-08-03
Hi Experts,

Please consider the code snippet below.

When a combo box selection is made I want that value (cboManpower.Text) along with another value (txtIDNumber.Text ) from the same form to be written to the tblPersonel tabel of the Manpower.accb database.

I am receiving an error saying that I can not reference the property of a control unless it has focus.



Private Sub cboManpower_Change()

   Dim dbs as Database

   Set dbs = OpenDatabase("Manpower.accdb")

   dbs.Execute "INSERT INTO tblPersonel (IDNumber, FullName) VALUES (" & Me.txtIDNumber.Text & ", '" & Me.cboManpower.Text & "');"

   dbs.Close

End Sub
0
Comment
Question by:DColin
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 2000 total points
ID: 41740148
Try this - Remove the .Text property:

   dbs.Execute "INSERT INTO tblPersonel (IDNumber, FullName) VALUES (" & Me.txtIDNumber & ", '" & Me.cboManpower & "');"
0
 

Author Closing Comment

by:DColin
ID: 41740166
Thanks that has done it.

Do you know why this would work. How does  the interpreter know what property to use when it is not referenced?
0
 
LVL 75
ID: 41740194
Because there is a Default Property. In the case of a Text box ... it's the Value property.
The .Text property is more often associated associated with a combo box, whose Default Property is also the Value property.
The difference is ... the .Text property is the 'value' currently in a Text or Combo box ... *before* the Text or Combo Box is updated, ie before the AfterUpdate event occurs when you exit either ... at which time the new Value is in effect.  

So for example, if you are say replacing a value in a Text box by say typing over an existing value ... the .Text property will given you that text at any moment while typing.  The .Value property is still the original value ... until you exit the Text box.

The Focus issue is a special nuance of the .Text property.
 = = =
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 41740395
The only event you can use to refer to a controls .text property is the OnChange event of that specific control.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

885 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