Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can not write values to Db in Access form.

Posted on 2016-08-02
4
Medium Priority
?
75 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 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 48

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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 …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

664 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