Solved

Can not write values to Db in Access form.

Posted on 2016-08-02
4
71 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
[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
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 500 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 47

Expert Comment

by:Dale Fye (Access MVP)
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

Independent Software Vendors: 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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

738 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