Insert a field value using VBA when adding record using a data entry form (1 Viewer)

gangu

New member
Local time
Today, 05:51
Joined
Oct 26, 2014
Messages
9
Hi all,
I have a form for entry and some fields are computed or result of a query from another table. I have a function that looks up a value from another table like so
**********************************************************
Public Function GetTargetType() As Variant

GetTargetType = DLookup("type", "tblFormulations", "[tblFormulations!formulation]=Forms![frmNmsConsumptionEntry]![formulation]")

End Function

**********************************************************
Which works fine when I test in the immediate window.

Then I have this form event. This however does not insert this value when I am adding records using my continuous form.

**********************************************************
Private Sub Form_BeforeInsert(Cancel As Integer)

Me!target_group = GetTargetType()
'Forms!frmNmsConsumptionEntry!target_group = GetTargetType()
'[tblNmsConsumption.target_group] = GetTargetType()

End Sub
**********************************************************
Need help with making sure I can insert this value once retrived and suggestions on the best way to solve this are welcome
 

llkhoutx

Registered User.
Local time
Today, 07:51
Joined
Feb 26, 2001
Messages
4,018
sub FormName_OnCurrent()
Forms!frmNmsConsumptionEntry!target_group = GetTargetType()
end sub
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 22:51
Joined
Jan 20, 2009
Messages
12,856
Looking up another field value based on an entry sounds like a normalization error to me.

BTW "Type" is a VBA reserved word and best avoided for object names.
 

JHB

Have been here a while
Local time
Today, 14:51
Joined
Jun 17, 2012
Messages
7,732
Keeping in mind what Galaxiom wrote:
If formulation is a text field type then:
Code:
GetTargetType = DLookup("type", "tblFormulations", "[formulation]='" & Forms![frmNmsConsumptionEntry]![formulation] & "'")
If number:
Code:
GetTargetType = DLookup("type", "tblFormulations", "[formulation]=" & Forms![frmNmsConsumptionEntry]![formulation])
 

gangu

New member
Local time
Today, 05:51
Joined
Oct 26, 2014
Messages
9
I have tried your suggestions but no luck yet, it does not insert when creating a record. I get the normalization hint but the reason why I want it included is purely because of the client....I read something like I have to use ADO when using vba to set values......how can I do that? Thanks in advance
 

smig

Registered User.
Local time
Today, 15:51
Joined
Nov 25, 2009
Messages
2,209
Keeping in mind what Galaxiom wrote:
Can you explain in plain English what are you trying to do
 

RainLover

VIP From a land downunder
Local time
Today, 22:51
Joined
Jan 5, 2009
Messages
5,041
I have tried your suggestions but no luck yet, it does not insert when creating a record. I get the normalization hint but the reason why I want it included is purely because of the client....I read something like I have to use ADO when using vba to set values......how can I do that? Thanks in advance

What does this mean?

Are you saying that the client is helping develop the database.

I get the normalization hint
It appears to me that you don't get the hint.

I agree with Galaxiom and most professional developers. Suggest you rethink.
 

Users who are viewing this thread

Top Bottom