Solved

Can not write values to Db in Access form.

Posted on 2016-08-02
4
42 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 - Access MVP) earned 500 total points
Comment Utility
Try this - Remove the .Text property:

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

Author Closing Comment

by:DColin
Comment Utility
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

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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)
Comment Utility
The only event you can use to refer to a controls .text property is the OnChange event of that specific control.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now