473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Using a VBA "If... Then... Else" Statement in a Form's AfterUpdate Event Procedure

MatthewML
I am attempting to insert a custom AfterUpdate Event Procedure into a text box on a form that I am designing in MS Access 2000. This text box contains the e-mail address of the referenced contact, and my goal is to automatically insert the "@abc.xyz" portion of the e-mail data string if and only if the user does not specify this part of the data string. I am attempting to do this because 99% of the e-mail addresses that I will be entering into the database will reference the same e-mail server domain, and I do not want the user to need to type out the entire e-mail address each time they input a new contact. However, I need the code to recognize if the user specifies a unique domain (i.e. a form of @abc.xyz that differs from the other 99% of contacts in the database), and in the case that the user does do so, the code must not insert anything into the text string. I assume that the easiest way of doing this is to make the code detect the presence of the "@" sign in the text box, and therein determine what action it takes based on whether or not the "@" sign is found. I am attempting to write a short bit of VBA code to accomplish this task, but I have thus far met with little success. When I attempt to test the code, I keep on getting an error describing a "type mismatch" and I need to debug.

Below is the code that I have written in its current state:

Private Sub E_Mail_AfterUpdate()
If E_Mail = " Like " * [@] * " " Then
E_Mail = StrConv(E_Mail, vbLowerCase)
Else
E_Mail = StrConv(E_Mail, vbLowerCase) + "@binghamton.edu"
End If
End Sub

The debugger constantly detects a problem in the second line of the code, and I believe that my use of a "Like" statement here may be incorrect. I am a novice at writing VBA code, and any help that I could receive in doing so would be very useful.

- Matthew
Jun 21 '07 #1
2 11911
nico5038
3,080 Expert 2GB
I wouldn't probably use this mechanism as it might confuse users.
Better perhaps to have separate email fields for own (with appending) and foreign email-addresses. Then you can use the BeforeUpdate event of the form to append the @ string and/or move the foreign emailaddress.

For the IF I would use:
Expand|Select|Wrap|Line Numbers
  1. Private Sub E_Mail_AfterUpdate()
  2. If Right(Me!E_Mail,1) = "@" Then
  3.   Me!E_Mail = Me!E_Mail & "binghamton.edu"
  4. End If
  5. End Sub
  6.  
or when no "@" is required:
Expand|Select|Wrap|Line Numbers
  1. Private Sub E_Mail_AfterUpdate()
  2. If NOT Instr(Me!E_Mail,"@") > 0 Then
  3.   Me!E_Mail = Me!E_Mail & "@binghamton.edu"
  4. End If
  5. End Sub
  6.  
Nic;o)
Jun 21 '07 #2
FishVal
2,653 Expert 2GB
I am attempting to insert a custom AfterUpdate Event Procedure into a text box on a form that I am designing in MS Access 2000. This text box contains the e-mail address of the referenced contact, and my goal is to automatically insert the "@abc.xyz" portion of the e-mail data string if and only if the user does not specify this part of the data string. I am attempting to do this because 99% of the e-mail addresses that I will be entering into the database will reference the same e-mail server domain, and I do not want the user to need to type out the entire e-mail address each time they input a new contact. However, I need the code to recognize if the user specifies a unique domain (i.e. a form of @abc.xyz that differs from the other 99% of contacts in the database), and in the case that the user does do so, the code must not insert anything into the text string. I assume that the easiest way of doing this is to make the code detect the presence of the "@" sign in the text box, and therein determine what action it takes based on whether or not the "@" sign is found. I am attempting to write a short bit of VBA code to accomplish this task, but I have thus far met with little success. When I attempt to test the code, I keep on getting an error describing a "type mismatch" and I need to debug.

Below is the code that I have written in its current state:

Private Sub E_Mail_AfterUpdate()
If E_Mail = " Like " * [@] * " " Then
E_Mail = StrConv(E_Mail, vbLowerCase)
Else
E_Mail = StrConv(E_Mail, vbLowerCase) + "@binghamton.edu"
End If
End Sub

The debugger constantly detects a problem in the second line of the code, and I believe that my use of a "Like" statement here may be incorrect. I am a novice at writing VBA code, and any help that I could receive in doing so would be very useful.

- Matthew
Hi!
Thanks for your question being detailed and clear.
Your problem is just a syntax issue.

Replace
Expand|Select|Wrap|Line Numbers
  1. If E_Mail = " Like " * [@] * " " Then
  2.  
with
Expand|Select|Wrap|Line Numbers
  1. If E_Mail Like "*@*" Then
  2.  
Good luck.
Jun 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jason Charalambides | last post by:
I set a program to automatically load values from a temporary file. However, there is a chance that the specific temporary file "C:\Temp\TU.tmp" may not exist at all. In that case I want that...
2
by: Josh Strickland | last post by:
I am attempting to create an Access database which uses forms to enter data. The issue I am having is returning the query results from the Stored Procedure back in to the Access Form. ...
2
by: jv | last post by:
Hello, I have a Service Cancellation form that contain a CancelDate text box. The text box has an AfterUpdate event that perform a few calculations. This works fine. However, next to the text...
5
by: Add and Paste | last post by:
Dear Access developers, I have a lot of cut and pasting to do from Excel to an Access form. But instead of just pasting, I want to "add then paste" the value. Does anybody have a way to do this?...
8
by: Carl | last post by:
Hi, Using MS Access 2000, is it possible to run a UPDATE or INSERT SQL query using some form of conditional IF THEN ?? for example: SELECT * FROM Books IF EXISTS(Select Books.ID = 1)
1
by: jburris | last post by:
I am completely new to VBA. I am trying to update a yes/no box in a subform based on a value that I call from the main form into the subform. Below is the if /then statement i am using: If...
5
by: krwill | last post by:
I'm trying to automate a combo box to add a record to the source table if it's "Not In List". I've tried many different examples and none have worked. Combo Box Name = Combo24 Source Table...
1
by: AccessGuy | last post by:
Happy New Year All! I have 2 queries, a qry_HISTData and qry_RAWData. qry_RAWData is automatically triggered from a subform using the "asofdate" of the mainform as a date parameter. What I'm...
4
kcdoell
by: kcdoell | last post by:
Hello: I have the following afterupdate event: Private Sub GWP_AfterUpdate() 'Updates the Total calculation in the control "SumGWP" on the quick reference 'table that is located on the form...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.