473,406 Members | 2,378 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,406 software developers and data experts.

Table Search from LostFocus

67
Hello Everyone

I have tblSales Which has CustomerID, Address1, Address2, Town, County, Postcode, Phone, DateOfVisit Fields and Paid chkbox. I have frmNewSales which is linked to tblSales.

What i am trying to do is:- when a user enters a Postcode into the postcode field and tabs away from the field. I want the OnLostFocus event to search tblSales to find any records with that postcode and if the DateOfVisit is over 30 days and Paid is unchecked it will show a msgbox "Outstanding Balance"

Hope this makes sense

Regards
Wayne
Feb 9 '09 #1
10 1812
ChipR
1,287 Expert 1GB
You may have to create a recordset and step through it like:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT * FROM tblSales WHERE PostCode = " & Postcode
  2. Set records = DBEngine(0)(0).OpenRecordset(strSQL)
  3. strMessage = ""
  4. While not records.EOF
  5.   If records!Paid = False AND records!DateOfVisit < (Date - 30) then
  6.     strMessage = strMessage & VbCrLf & records!CustomerID
  7.   End If
  8. Wend
  9. If strMessage > "" Then
  10.   MsgBox "Outstanding Balance: " & strMessage
  11. End If
I think checkbox fields can be compared to True/False. You could also filter out the Paid and Date fields in the SQL string, but either way, this should give you the general idea.
Feb 9 '09 #2
Wayneyh
67
Thanks ChipR

I will try it and let you know if it works for me

Regards

Wayne
Feb 9 '09 #3
Wayneyh
67
Hi ChipR

I tried the code below but line 3 came back with runtime error

Please advise
Expand|Select|Wrap|Line Numbers
  1. Private Sub Postcode_LostFocus()
  2. strSQL = "SELECT * FROM tblSales WHERE PostCode = " & Postcode
  3. Set records = DBEngine(0)(0).OpenRecordset(strSQL)    -    Runtime error 3061
  4. strMessage = ""
  5. While Not records.EOF
  6.   If records!Paid = False And records!DateOfVisit < (Date - 30) Then
  7.     strMessage = strMessage & vbCrLf & records!InvoiceNumber
  8.   End If
  9. Wend
  10. If strMessage > "" Then
  11.   MsgBox "Outstanding Balance: " & strMessage
  12. End If
  13.  
  14. End Sub
  15.  
  16. Regards 
  17. Wayne
Feb 10 '09 #4
Probably not the best / quickest method, but I'd make a query here. Something like:
Expand|Select|Wrap|Line Numbers
  1. SELECT CustomerID 
  2. FROM tblSales 
  3. WHERE Postcode=[forms]![frmNewSales]![PostCode] And CustomerID=[Forms]![frmNewSales]![CustomerID] And DateOfVisit >Date()-30 AND Paid=0;
I'm no SQL genius, that might need a little fudging to get it right.
That's based on your postcode text box being called 'PostCode', and also that you have the customer ID field on the form too (even if it's hidden or locked) called 'CustomerID'.
Then the code for your OnLostFocus would be similar to
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(DLookup("CustomerID","QueryName")) Then
  2. MsgBox "Outstanding Balance!"
  3. End If
Feb 10 '09 #5
Wayneyh
67
Hello mandanarchi

I can't get your method to work at all,

Any ideas

Wayne
Feb 10 '09 #6
Darnit. Just spotted a mistake.
The >Date()-30 should be <Date()-30

Try that and see if that one works.
Feb 10 '09 #7
Wayneyh
67
Thanks mandanarchi

That works great for me

Wayne
Feb 10 '09 #8
No problem =)
Glad I could help.
Feb 10 '09 #9
missinglinq
3,532 Expert 2GB
Note that you should probably place this kind of code in the AfterUpdate event of the textbox rather than the OnLostFocus event.

Doing this will cause the message box to appear only if you enter/edit the PostCode field.

Using the OnLostFocus event will bring up the message box even if you tab thru the field without entering /editing the postcode.

Linq ;0)>
Feb 10 '09 #10
NeoPa
32,556 Expert Mod 16PB
Good point there by Linq. I strongly advise you take note of it Wayne.

@Mandi:
That's good stuff. Nice to see you offering answers here :)

I would offer a similar solution which avoids the need for creating a query specially :
Expand|Select|Wrap|Line Numbers
  1. Dim strWhere As String
  2.  
  3. strWhere = "([PostCode]='" & Me.PostCode & "') AND " & _
  4.            "([DateOfVisit]<(Date()-30)) AND " & _
  5.            "(NOT [Paid])"
  6. If Not IsNull(DLookup("[CustomerID]", "[tblSales]", strWhere)) Then
  7.   MsgBox "Outstanding Balance!"
  8. End If
Feb 16 '09 #11

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

Similar topics

4
by: MJW | last post by:
Is there a way for me to know if or which command button was just clicked that triggers the LostFocus event for the current control on a Form? I have a form that has many types of users who each...
2
by: vooose | last post by:
Does anyone know the difference between these two? The doc for LostFocus says 'Occurs when the control loses focus.' whereas for Leave it says 'Occurs when the input focus leaves the control'...
4
by: dudzcom | last post by:
hi, this is the first time i've posted to this board, though i have been a devoted lurker for some time i have been working in vb.net and run into a couple problems that seem to require solutions...
4
by: Bernard Bourée | last post by:
I have some TextBoxes created by code. I want to write a procedure to handle the lost of focus of any of these TextBox. How should I do that ? Thanks --
4
by: Wayne Wengert | last post by:
I have a form that has a cbo set to TabIndex 0 (it is the only item set to tabindex 0). On page Load if I step through the code that cbo LostFocus event fires after other initializations are...
6
by: Carmon | last post by:
I am using Managed C++ with a CFormView and a MaskedTextBox and have used MAKE_DELEGATE to sink the LostFocus event of this control. I have other events I've sinked off this control and they all...
18
by: TORQUE | last post by:
Hi, Im wondering if anyone can help me with a problem. I have a form with more than 50 unbound fields. Some of the fields will be blank from time to time. This seems to be where im having...
14
by: teddysnips | last post by:
WINDOWS FORMS I've a form that has a textbox that allows the user to enter a string. On the LostFocus event, the textbox formats the string into a preferred format. However, if the user...
2
by: Jason Huang | last post by:
Hi, In my .Net 1.1 C# windows form Form1 I have nothing but 2 TextBox controls on the Form1, the T1 and T2. I would like to test the sequence of the GotFocus LostFocus things, so I have the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.