VBA code (1 Viewer)

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
Hi
Please help me to correct the vba code below:
I have 4 field: Date1,Date2,Date3,Type and a 1 text box called "Total" and below is the vba code which is not working as per the target output:

Private Sub Total_AfterUpdate()
If Me.Type = A Then Me.Total = Me.Date3 - Me.Date2
Else
Me.Total = Me.Date3 - Me.Date1
End If
End Sub

The target to the vba code is that:
- when the value of field "type" is A, the text box "Total" should compute field "Date3-Date2"
- when the value of field "type" is B, the text box "Total" should compute field "Date3-Date1"

Having hard time how to add the value B in the equation.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 05:27
Joined
Apr 27, 2015
Messages
6,331
What kind of error/output are you getting?
 

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
Im not getting error. But the formula is not working
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 05:27
Joined
Apr 27, 2015
Messages
6,331
I noticed that your AfterUpdate is on the wrong control as well. How/when is the Type field populated as well as the Date fields?
 

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
Thanks Nautica

Date1 to Date 3 to be filled , then the type field...
 

Gasman

Enthusiastic Amateur
Local time
Today, 10:27
Joined
Sep 21, 2011
Messages
14,267
Pay attention to post 4
 

Minty

AWF VIP
Local time
Today, 10:27
Joined
Jul 26, 2013
Messages
10,371
Be aware that Type is a reserved word in Access.
I would change it to something more descriptive CarType or BombType etc.
 

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
The value A or B, is a value that I manually put in the field "type"
 

Minty

AWF VIP
Local time
Today, 10:27
Joined
Jul 26, 2013
Messages
10,371
So as per @NauticalGent you have put your code in the after update event of the total.
It should be in the after update event of the [type] field surely?

Also, this looks like it could always be calculated, so in theory, you shouldn't store it?
 

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
So as per @NauticalGent you have put your code in the after update event of the total.
It should be in the after update event of the [type] field surely?

Also, this looks like it could always be calculated, so in theory, you shouldn't store it?

Taking guys advise, I change the type filename to TypeBrand and change the AfterUpdateLocation
Private Sub TypeBrand_AfterUpdate()
If Me.TypeBrand = A Then Me.Total = Me.Date3 - Me.Date2
Else
Me.Total = Me.Date3 - Me.Date1
End If
End Sub


And below is the error code:


1625850948935.png
 

Isaac

Lifelong Learner
Local time
Today, 02:27
Joined
Mar 14, 2017
Messages
8,777
Use the normal layout:

Code:
If Something Then
    Somethingelse
Else
    Somethingelse
End if

It's not typical to use the one-liner, although some people enjoy using it, and you can do that if the whole statement is on one line and you might need a colon too, I can't remember b/c I never use it that way
 

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
Hi,

Thanks for several tips.

It works as below.
Private Sub TypeBrand_AfterUpdate()
If Me.TypeBrand = A Then
Me.Total = Me.Date3 - Me.Date2
Else
Me.Total = Me.Date3 - Me.Date1
End If
End Sub

For now the code is OK as there are only two brand which is A and B.
In the future if there is additional brand let say C, then I need to adjust it again which I don't know how to code it.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 05:27
Joined
Apr 27, 2015
Messages
6,331
Hi,

Thanks for several tips.

It works as below.


For now the code is OK as there are only two brand which is A and B.
In the future if there is additional brand let say C, then I need to adjust it again which I don't know how to code it.
Have a look at the Select Case feature.

Edit: You should also take Minty's advice about not storing a calculated value in a table.
 

Isaac

Lifelong Learner
Local time
Today, 02:27
Joined
Mar 14, 2017
Messages
8,777
Yeah, what @NauticalGent said, and also (just so you're aware), there's always this:

Code:
if something then
   some result
elseif something then
   some result
elseif ...then........infinitely
   some result
end if
 

missinglinq

AWF VIP
Local time
Today, 05:27
Joined
Jun 20, 2003
Messages
6,423
Paying close attention to everything else you've been told...if you're entering an A or B into the Type control:

If Me.TypeBrand = A

then it has to be Text and the code needs to be

If Me.TypeBrand = "A"

Linq ;0)>
 
Last edited:

Valient

Member
Local time
Today, 12:27
Joined
Jun 21, 2021
Messages
48
So...did that resolve the problem?

Linq ;0)>
Yes, it works this way..Thanks

Private Sub TypeBrand_AfterUpdate()
If Me.TypeBrand = "A" Then
Me.Total = Me.Date3 - Me.Date2
ElseIf Me.TypeBrand = "B" Then
Me.Total = Me.Date3 - Me.Date1
Else
Me.Total = Null
End If
End Sub

Resolved!
 

Users who are viewing this thread

Top Bottom