Branislav ,
Me.fldUBoundField = DLookup("[fldFieldName]", "tblTableName",
"[IDField] = " & Something)
You will got a value in Unbound field which you can change (retype) as now DLookup is in the code.
I did that before, directly in TextBox.ControlSource but then I
couldn't retype the data. You say that if I use an event to populate
the TextBox (under VBA) it would allow me to edit the TextBox
(retype)?
When you do that, use AfterUpdate event of that field to write data backto second table using either SQL or recordset or even query if that is easier for you to create.
I am interested in the query details. You mean, I can establish a
query (TextBox.ControlSource = QueryName) after having modified the
TextBox? Establising the Query after having changes the TextBox
content wouldn't revert the TextBox content to the original register/
field data?
As I said, I tried once again the SubForm and it worked. It was quite
messy to solve the graphics/layout details because the SubForm should
simulate a simple TextBox. I had to disable many bound graphics
elements. Even so, it was difficult to adjust graphic placement.
Thanks.
H. Martins
Hi,
I will give you a sample how it may work, and you can change it
depending on your form design.
You have a form and you want to see some field data from second table
in one text box which is somehow related to the current. Put a text
box on the form. Leave the Control Source of the text box empty. That
makes text box unbound - it does not have record source.
If your run the form you will see that field empty and you can type
anything into it. Content of the field will never auto update as it is
not related to table field nor it is value is controlled by VBA code.
OK, revert to design view. Select form event OnCurrent and create
[Event Procedure]. This will open VBA screen to write a code related
to record change. Write a code similar to this (change table and field
name to your 2nd table name and field from that table):
Me.fldUBoundField = DLookup("[fldFieldName]", "tblTableName", _
"[IDField] = " & Something)
fldUBoundField is the unbound text box name
[fldFieldName] is the field name of 2nd table
tblTableName is the the name of 2nd table
[IDField] is the ID field of 2nd table
"Something" is usually ID field of form record source table
* Above command will read the value of [fldFieldName] from 2nd table
named "tblTableName", where ID field of that table is the same as ID
of form record source table
Finally, you will have the code similar to this:
Private Sub Form_Current()
Me.fldUBoundField = DLookup("[fldFieldName]", "tblTableName", _
"[IDField] = " & Something)
End Sub
Now, if you run the form and change record you will see that field
show data in unbound text box, but you can retype that value. Program
will not update value in second table, so you need code to update data
in second table. Back to design view. Click that field and select
event AfterUpdate. This will run the code when you update value:
Private Sub fldUBoundField_AfterUpdate()
On Error GoTo ErrHandler
Dim strSQL As String
strSQL = "UPDATE 2ndFieldName SET 2ndTableName.2ndFieldName = " _
& [Forms]![CurrentFormName]![fldUBoundField] _
& " WHERE 2ndTableName.ID = " _
& [Forms]![CurrentFormName]![ID]
CurrentDb.Execute strSQL, dbFailOnError
Exit Sub
ErrHandler:
MsgBox Err.Description
End Sub
When you update value of fldUBoundField it will run above code. By
lines it will do the following:
1 - if error arise goto "ErrHandler:"
2 - dim the variable strSQL (str means string)
3-6 - this is SQL command similar to query in SQL view
7 - run the SQL to update value in 2nd table. Raise error if the SQL
fails.
8 - Leave the code
9-10 - Create a message box with error description i.e. if the table
is locked for updating (like when someone else is editing same data
and the record is locked) warn the user that update has failed.
Also you can have a query to update 2nd table field. In that case code
will look like this:
Private Sub fldUBoundField_AfterUpdate()
DoCmd.OpenQuery "QueryName"
End Sub
---
Of course, if you use subform you don't need above code, but you will
need to control and filter record source of the subform to show
correct data regarding record you are viewing in main form.
To conclude: you will add unbound text box control on the form,
without anything in ControlSource, and you will control, using code,
which data you will see and also the code will take care to update
field value. Does it makes sense now?
Regards,
Branislav Mihaljev
Microsoft Access MVP