473,387 Members | 1,844 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,387 software developers and data experts.

Help with IF AND IF

147 100+
I have a form that contains the following fields:
EmployeeID
OTDate
HoursPay
RateOfPay
TypeOfJob
Notes

I am trying to write a code that upon lost focus of the Notes field will check the TypeOfJob field to see if its value is "Vacation" and if so to check the HoursPay field to see if its value is greater than 8 and iff so to create a new record copying the data of the current records EmployeeID, OTDate, RateOfPay, and Notes fields and set the value of the TypeOfJob field to "112Vacation" and set the value of the HoursPay field to the remaining value over 8 hours that was originally set in the previous record.

For example:
If I enter the following in my form:
EmployeeID: 5555
OTDate:01/14/2009
HoursPay: 12
RateOfPay: Supervisor
TypeOfJob: Vacation
Notes: Smiths vac.

Then after exiting the Notes field a new record should be created as follows:
If I enter the following in my form:
EmployeeID: 5555
OTDate:01/14/2009
HoursPay: 4
RateOfPay: Supervisor
TypeOfJob: 112Vacation
Notes: Smiths vac.

Thanks
Dan
Jan 14 '09 #1
28 1968
ChipR
1,287 Expert 1GB
I would suggest something like a "Submit" button, so users know when they are triggering it, rather than on lost focus. Say they don't fill in the fields in order or something?

Expand|Select|Wrap|Line Numbers
  1. dim strSQL as String
  2.  
  3. if blah blah blah
  4. strTypeOfJob = "1" & HoursPay & "Vacation"  'Not sure where the "1" comes from
  5. intHoursPay = HoursPay - 8
  6.  
  7. strSQL = "INSERT INTO myTableName VALUES (" & EmployeeID _
  8.  & ", #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay _
  9.  & """, """ & strTypeOfJob & """, """ & Notes & """)"
  10. Currentdb.Execute strSQL, dbfailonerror 
  11.  
  12. End If
  13.  
Some assumptions here that could change the syntax of the SQL statement:
EmployeeID is an Int
OTDate is a Date
HoursPay is an Int
The rest are Strings
Those are all the fields in your table, otherwise you would need to enumerate them before the Values in the Insert statement.
Jan 14 '09 #2
DAHMB
147 100+
Thanks for the help I am a self taught novice here so please deal with me.

Employee ID is text
you are correct with your other assumptions.

Could you dumb it down for me a bit, could you give me an idea of what the If statement should look like?
Also in
< strTypeOfJob = "1" & HoursPay & "Vacation" 'Not sure where the "1" comes from
intHoursPay = HoursPay - 8 >

Is that the code I need to put in or are you aking me for clearification about the 1?
Thanks
Dan
Jan 14 '09 #3
ChipR
1,287 Expert 1GB
In strTypeOfJob = "1" & HoursPay & "Vacation" that is the code if you just want to paste a 1 in front of the HoursPay and put "Vacation" after it. But yeah I wasn't sure exactly what you wanted there.

Expand|Select|Wrap|Line Numbers
  1.  
  2. If TypeOfJob = "Vacation" and HoursPay > 8 then
  3. ...
  4. End If
  5.  
Well that wasn't as complicated as I thought when I first read your description, sorry.


If the EmployeeID is text, it will need extra " around it in the string. So you do this by putting in "". The new strSQL would be:
strSQL = "INSERT INTO myTableName VALUES (""" & EmployeeID _ & """, #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay _
& """, """ & strTypeOfJob & """, """ & Notes & """)"

and when it's evaluated it will be
INSERT INTO myTableName VALUES ("EmployeeID", #OTDate#, intHoursPay, "RateOfPay", "strTypeOfJob", "Notes")
Jan 14 '09 #4
DAHMB
147 100+
Ok so far I have this but it is not correct because I think I confused you on the fields:
<Code>
1. If [TypeOfJob] = "Vacation" And [HoursPay] > 8 Then
2. strTypeOfJob = "1" & HoursPay & "Vacation"
3. intHoursPay = HoursPay - 8
4. strSQL = "INSERT INTO myTableName VALUES (""" & EmployeeID & """, #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay & """, """ & strTypeOfJob & """, """ & Notes & """)"
5. End If
<End Code>

Question:
What is the "1" in Line two
What do you mean by "and when it's evaluated it will be
INSERT INTO myTableName VALUES ("EmployeeID", #OTDate#, intHoursPay, "RateOfPay", "strTypeOfJob", "Notes")"? Where do I plug this in?

Thanks
Dan
Jan 14 '09 #5
ChipR
1,287 Expert 1GB
The "1" in line 2 is because you wanted "112Vacation". I assumed the 12 was related to hours and the 1 was there for some other reason. But you can set strTypeOfJob = whatever you want using the same methods.

With the evaluated string, I was just trying to explain why you use the extra "". That is the string that will be executed when the code runs. Just put
Expand|Select|Wrap|Line Numbers
  1.  strSQL = "INSERT INTO myTableName VALUES (""" & EmployeeID _
  2.  & """, #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay _
  3.  & """, """ & strTypeOfJob & """, """ & Notes & """)"
in your code.

Sorry if I was confusing.
Jan 14 '09 #6
DAHMB
147 100+
Okay here is what I have but I get a variable not defined error on strTypeOfJob in line 2

<Code>
1. If [TypeOfJob] = "Vacation" And [HoursPay] > 8 Then
2. strTypeOfJob = "112Vacation" & HoursPay & "Vacation" 'Not sure where the "1" comes from
3. intHoursPay = HoursPay - 8
4. strSQL = "INSERT INTO tblOvertime VALUES (""" & EmployeeID & """, #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay & """, """ & strTypeOfJob & """, """ & Notes & """)"
5. End If
<End Code>

Thanks Again
Dan
Jan 14 '09 #7
ChipR
1,287 Expert 1GB
At the beginning of your code
Expand|Select|Wrap|Line Numbers
  1. Dim strTypeOfJob as String
  2. Dim intHoursPay as Integer
Since we have to Dim all variables before we use them.
Do you always want to set the TypeOfJob = "112Vacation"? If so then just do
Expand|Select|Wrap|Line Numbers
  1. strTypeOfJob = "112Vacation"
The other stuff was because I thought it was variable.
Jan 14 '09 #8
DAHMB
147 100+
Ok thanks now my code looks like the following and I am getting a variable not defined error on strSQL in line 6 how do I dim that one?

<Code>
1. Dim strTypeOfJob As String
2. Dim intHoursPay As Integer
3. If [TypeOfJob] = "Vacation" And [HoursPay] > 8 Then
4. strTypeOfJob = "112Vacation" & HoursPay & "Vacation"
5. intHoursPay = HoursPay - 8
6. strSQL = "INSERT INTO tblOvertime VALUES (""" & EmployeeID & """, #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay & """, """ & strTypeOfJob & """, """ & Notes & """)"
7. End If
<End Code>
Jan 14 '09 #9
ChipR
1,287 Expert 1GB
strSQL is a string variable so at the top have
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL as String
along with the other Dim statements.
Jan 14 '09 #10
DAHMB
147 100+
Ok now here is my code and it does not work
I wanted it to create a new record with the data from the old with the cahnges but nothin happens I tried it with a button and with on lost focus but no new record is created. Why?

Dim strTypeOfJob As Integer
Dim strSQL As String
Dim intHoursPay As Integer
If [TypeOfJob] = "1" And [HoursPay] > 8 Then
strTypeOfJob = "99"
intHoursPay = [HoursPay] - 8
strSQL = "INSERT INTO tblOvertime VALUES ("" & EmployeeID & "", #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay & """, """ & strTypeOfJob & """, """ & Notes & """)"
End If
Jan 14 '09 #11
ChipR
1,287 Expert 1GB
You still need the
Expand|Select|Wrap|Line Numbers
  1. Currentdb.Execute strSQL, dbfailonerror
right before the End If.
You put together the strSQL, but you didn't execute it to make any changes to the database.
Jan 14 '09 #12
DAHMB
147 100+
Okay it is almost working, now I am getting a datatype mismatch error does anything pop out at you?

Dim strTypeOfJob As Integer
Dim strSQL As String
Dim intHoursPay As Integer
If [TypeOfJob] = "1" And [HoursPay] > 8 Then
strTypeOfJob = "99"
intHoursPay = [HoursPay] - 8
strSQL = "INSERT INTO tblOvertime VALUES (""" & EmployeeID & """, #" & OTDate & "#, " & intHoursPay & ", """ & RateOfPay & """, "" & strTypeOfJob & "", """ & Notes & ""","" & ID & "")"
CurrentDb.Execute strSQL, dbFailOnError
End If


My fields are as follows:
EmployeeID = integer
OTDate = Date
HoursPay = integer
RateOfPay = currency
TypeOfJob = integer
Notes = text
ID =autonumber


Thanks for all the help
Dan
Jan 14 '09 #13
ChipR
1,287 Expert 1GB
Based on those datatypes, you need:
strSQL = "INSERT INTO tblOvertime VALUES (" & EmployeeID & ", #" & OTDate & "#, " & intHoursPay & ", " & RateOfPay & ", " & strTypeOfJob & ", """ & Notes & """," & ID & ")"
Jan 14 '09 #14
ChipR
1,287 Expert 1GB
Based on those datatypes, you need:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO tblOvertime VALUES (" & EmployeeID _
  2.  & ", #" & OTDate & "#, " & intHoursPay & ", " & RateOfPay _
  3. & ", " & strTypeOfJob & ", """ & Notes & """," & ID & ")"
Jan 14 '09 #15
ChipR
1,287 Expert 1GB
Based on those datatypes, you need:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "INSERT INTO tblOvertime VALUES (" & EmployeeID _
  2.  & ", #" & OTDate & "#, " & intHoursPay & ", " & RateOfPay & ", " _
  3.  & strTypeOfJob & ", """ & Notes & """," & ID & ")"
Sorry, forums didn't seem to be working.
Jan 14 '09 #16
DAHMB
147 100+
Hi thanks but I am still getting the datatype mismatch could it be the id field that is an autonumber?
When I go to the highlighted section it lists dbFailOnError = 128

my code is:
<Code>
Dim strTypeOfJob As Integer
Dim strSQL As String
Dim intHoursPay As Integer
If [TypeOfJob] = "1" And [HoursPay] > 8 Then
strTypeOfJob = "99"
intHoursPay = [HoursPay] - 8
strSQL = "INSERT INTO tblOvertime VALUES (" & EmployeeID _
& ", #" & OTDate & "#, " & intHoursPay & ", " & RateOfPay & ", " _
& strTypeOfJob & ", """ & Notes & """," & ID & ")"
CurrentDb.Execute strSQL, dbFailOnError
End If
<End Code>
Jan 15 '09 #17
ChipR
1,287 Expert 1GB
Try this, now that we know TypeOfJob is an Integer and not a String.
Change

Dim strTypeOfJob As Integer
to
Dim intTypeOfJob as Integer

strTypeOfJob = "99"
to
intTypeOfJob = 99

and in the strSQL = "...", change strTypeOfJob to intTypeOfJob

I believe that will do it.
Jan 15 '09 #18
DAHMB
147 100+
I am now getting a Runtime error 3464: Data type mismatch in criteria expression:
CurrentDb.Execute strSQL, dbFailOnError
Jan 15 '09 #19
DAHMB
147 100+
Any ideas on why it is returning this error?
Runtime error 3464: Data type mismatch in criteria expression:
CurrentDb.Execute strSQL, dbFailOnError
Jan 15 '09 #20
ChipR
1,287 Expert 1GB
Can you paste your current code? Those are all of the fields in the table right?
Jan 15 '09 #21
DAHMB
147 100+
Thanks my fields and their type are now as follows and yes there are no other fields then listed below:

EmployeeID - Text
OTDate - Date
HoursPay - Double
RateofPay - Currency
TypeOfJob - integer
Notes - text
ID - Autonumber

My Code is as follows:

<Code>
Dim intTypeOfJob As Integer
Dim strSQL As String
Dim dblHoursPay As Double
If [TypeOfJob] = 1 And [HoursPay] > 8 Then
intTypeOfJob = 99
dblHoursPay = [HoursPay] - 8
strSQL = "INSERT INTO tblOvertime VALUES (""" & EmployeeID _
& """, #" & OTDate & "#, " & dblHoursPay & ", " & RateOfPay & ", " _
& intTypeOfJob & ", """ & Notes & """," & ID & ")"
CurrentDb.Execute strSQL, dbFailOnError
End If
<End Code>
Jan 15 '09 #22
ChipR
1,287 Expert 1GB
Last time listed the fields you had EmployeeID as Integer, so is this correct? The syntax looks ok to me if those are right, and you don't need to list the fieldnames before the values if you are putting something in each field.
Jan 15 '09 #23
DAHMB
147 100+
Yes the employee field is asctualy text. Any ideas why I am getting the error?
Jan 15 '09 #24
ChipR
1,287 Expert 1GB
You may have to put
MsgBox strSQL
right before the CurrentDB.Execute
and copy to here what it says in the message box when it comes up, because I can't tell what the problem could be. Sorry I just can't see anything wrong with the code.
Jan 15 '09 #25
DAHMB
147 100+
Ok the msg box says:

Insert INTO tblOvertime VALUES ("0418", #2/1/2009#, 8, 35.748, 99, "", 2102)
Jan 15 '09 #26
ChipR
1,287 Expert 1GB
Looks ok. Does it work if you put something in Notes by any chance?
Jan 15 '09 #27
DAHMB
147 100+
I got it someone pointed out we were not puting it anywhere the code as follows now works. Thank you for all you help.

<Code>
Private Sub Notes_LostFocus()
Dim intTypeOfJob As Integer
Dim strSQL As String
Dim dblHoursPay As Double
If [TypeOfJob] = 1 And [HoursPay] > 8 Then
intTypeOfJob = 21
dblHoursPay = [HoursPay] - 8
strSQL = "INSERT INTO tblOvertime (EmployeeID, OTDate, HoursPay, " & _
"RateofPay, TypeOfJob, Notes) " & _
"VALUES (""" & EmployeeID & """, #" & OTDate & "#, " & dblHoursPay & _
", " & RateOfPay & ", " & intTypeOfJob & ", """ & Notes & """)"
CurrentDb.Execute strSQL, dbFailOnError
End If
End Sub
<End Code>
Jan 15 '09 #28
ChipR
1,287 Expert 1GB
Glad it works. You know, I should have realize you probably can't insert an autonumber. Now I'll know if I use them. Thank you!
Jan 15 '09 #29

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.