PDA

View Full Version : Solved: How can I implement this in Access using vba?



keek2a4
04-09-2008, 04:21 AM
Hi everyone,

I wanted to know how to implement the following:

I have a results textbox in my form, and If I enter "p" or "P", it writes "Pass" in the box and saves "Pass" in the database, and if I enter "f" or "F" it writes "Fail" in the box and saves "Fail" in the database. Can this be done in access, using VBA?

Thanks,
Zub

CreganTur
04-09-2008, 08:36 AM
Doing this involves 3 steps:
1) check the value of the textbox
2) setting the value of the textbox to desired word
3) update record with new value

I suggest that you add a button to your form that you would click in order to add your pass/fail marker (this VBA is set to work with a button click)


'Check to see what value is in the result textbox (replace [result] with the name of the textbox you are using)
If Me![result] = "f" Then
Me![result] = "Fail" '<<<writes set value into the textbox
ElseIf Me![result] = "F" Then
Me![result] = "Fail" '<<<writes set value into the textbox
ElseIf Me![result] = "p" Then
Me![result] = "Pass" '<<<writes set value into the textbox
ElseIf Me![result] = "P" Then
Me![result] = "Pass" '<<<writes set value into the textbox
Else
Me![result] = "" '<<<writes an empty value into the textbox if anything other than "f","F","p", or"P" is entered
End If

'This will update the current record with your pass/fail marker
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

I've attached a sample database if you need to look at it.

Trevor
04-09-2008, 04:12 PM
Note: you don't need the 2 statements for "f" and "p" unless it is a vbBinarycompare(exact mach) argument, you only need 1 statment for "f" and "p" for pass or fail

keek2a4
04-10-2008, 02:03 AM
Thanks for your replies, I will look into the options. Regards, zub

CreganTur
04-10-2008, 05:07 AM
Note: you don't need the 2 statements for "f" and "p" unless it is a vbBinarycompare(exact mach) argument, you only need 1 statment for "f" and "p" for pass or fail

Thanks for the heads-up on that. I thought that the strings would be case sensitive, but it's good to know that it's not- unless an (exact match) is specified.

DarkSprout
04-10-2008, 08:08 AM
How about this for a very clean way:
Make a On Key Down Event from the text box ([Result])...
Private Sub Result_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 80 ' P
[Result].Text = "Pass"
Case 70 ' F
[Result].Text = "Fail"
End Select
KeyCode = 0
End Sub