473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to validate excel textbox values using Access VBA

126 New Member
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using the following "DateTextBox" class module).

Whenever a user enters in to the textbox, the code displays "Please enter date in mm/dd/yyyy format (for ex: 01/01/2009)." message which is accomplished using "MouseUp" event.

And, I used "Exit" event to validate the value that the user entered in to the textbox and display an error message if the user enter a date in past.

But somehow the "Exit" event is not working.

Please kindly let me know how to solve this issue?

Thanks.

1. Created an Access VBA module to open and create textboxes in an excel spreadsheet dynamically:

Expand|Select|Wrap|Line Numbers
  1. Public eXL As New eventsXL
  2.  
  3. Function Create_TextBox()
  4. Dim OLEObj As OLEObject
  5. Dim myRng As Range, myCell As Range
  6.  
  7.  
  8. With eXL
  9. If .XL Is Nothing Then Set .XL = New Excel.Application
  10. .XL.Visible = True
  11. .XL.Interactive = True
  12. Set .WB = .XL.Workbooks.Open("C:\Book1.xls", , False)
  13. Set .WS = .WB.Worksheets("Example")
  14. .WS.Activate
  15.  
  16. .XL.CommandBars("Control Toolbox").Visible = False
  17. .WS.OLEObjects.Delete
  18.  
  19. Set myRng = .WS.Range("A2:A4")
  20. For Each myCell In myRng.Cells
  21. With myCell
  22.     .NumberFormat = ";;;" 'hide the value in the cell
  23.     Set OLEObj = .Parent.OLEObjects.Add _
  24.                 (ClassType:="Forms.TextBox.1", Link:=False, _
  25.                 DisplayAsIcon:=False, _
  26.                 Top:=.Top, _
  27.                 Left:=.Left, _
  28.                 Width:=.Width, _
  29.                 Height:=.Height)
  30. End With
  31.  
  32. .AddTextBoxEvents OLEObj.Object
  33. Next myCell
  34. End With
  35. End Function
2. Created "eventsXL" class module to attach events to the created textboxes:

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents XL As Excel.Application
  2. Public WithEvents WB As Excel.Workbook
  3. Public WithEvents WS As Excel.Worksheet
  4. Private myTxb As New VBA.Collection
  5.  
  6. Public Property Get myTextBoxes() As VBA.Collection
  7.     Set myTextBoxes = myTxb
  8. End Property
  9.  
  10. Public Function AddTextBoxEvents(obj As Variant) As DateTextBox
  11.     Set AddTextBoxEvents = New DateTextBox
  12.     Set AddTextBoxEvents.myTextBox = obj
  13.     Me.myTextBoxes.Add AddTextBoxEvents
  14. End Function
  15.  
  16. Private Sub XL_WorkbookBeforeClose(ByVal WB1 As Excel.Workbook, Cancel As Boolean)
  17. With XL.Workbooks
  18. XL.Quit
  19. Set eXL.XL = Nothing
  20. Set eXL.WB = Nothing
  21. Set eXL.WS = Nothing
  22. Set eXL = Nothing
  23. End With
  24. Exit Sub
  25. End Sub
3. Created "DateTextBox" class module to define events that needs to attached to the created date textboxes:

Expand|Select|Wrap|Line Numbers
  1. Public WithEvents txb As MSForms.TextBox
  2.  
  3. Public Property Get myTextBox() As MSForms.TextBox
  4. Set myTextBox = txb
  5. End Property
  6.  
  7. Public Property Set myTextBox(ByRef myText As MSForms.TextBox)
  8. Dim curDate As String
  9. Set txb = myText
  10. curDate = Format(Date, "mm/dd/yyyy")
  11. With txb
  12.     .Text = curDate
  13. End With
  14. End Property
  15.  
  16. Public Sub txb_MouseUP(ByVal Button As Integer, _
  17.          ByVal Shift As Integer, ByVal X As Single, _
  18.          ByVal Y As Single)
  19. MsgBox "Please enter date in mm/dd/yyyy format (for ex: 01/01/2009)."
  20. End Sub
  21.  
  22. Public Sub txb_Exit(Cancel As Integer)
  23. Dim curDate As String, mon As String, day As String, year As String
  24. curDate = Format(Date, "mm/dd/yyyy")
  25. dParts = Split(curDate, "/")
  26. txbParts = Split(txb.Text, "/")
  27.  
  28. If (txbParts(0) <= dParts(0)) And (txbParts(1) < dParts(1)) And (txbParts(2) <= dParts(2)) Then
  29. MsgBox "Please enter either the current date or the date in future!"
  30. End If
  31. End Sub
Sep 11 '08 #1
1 6287
NeoPa
32,556 Recognized Expert Moderator MVP
Try using the following for your class code :
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public WithEvents txb As MSForms.TextBox
  4.  
  5. Public Property Get myTextBox() As MSForms.TextBox
  6.   Set myTextBox = txb
  7. End Property
  8.  
  9. Public Property Set myTextBox(ByRef myText As MSForms.TextBox)
  10.   Set txb = myText
  11.   txb.Text = Format(Date, "mm/dd/yyyy")
  12. End Property
  13.  
  14. Public Sub txb_MouseUP(ByVal Button As Integer, _
  15.                        ByVal Shift As Integer, _
  16.                        ByVal X As Single, _
  17.                        ByVal Y As Single)
  18.   MsgBox "Please enter date in mm/dd/yyyy format (EG. 01/14/2009)."
  19. End Sub
  20.  
  21. Public Sub txb_Exit(Cancel As Integer)
  22.   If Date > CDate(txb.Text) Then
  23.     MsgBox "Please enter a date that is not in the past!"
  24.     Calcel = True
  25.   End If
  26. End Sub
NB. It doesn't validate that the data entered is a valid date.
Sep 11 '08 #2

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

Similar topics

1
by: CapeCoder | last post by:
I'd like to use the Excel.WorksheetFunction library to compute median and percentiles in a user-defined function. Rather than creating a SQL dataset in the function, I'd like to use the data from...
9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
6
by: Paul | last post by:
I was wondering if anyone has had an issue where using vba code to read an excel file and import the data into an access table some records are not imported from the excel file. It seems looking at...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
3
by: Conrad F | last post by:
Hello All, I know how to import a specific named excel sheet into a datagrid using ADO.NET by setting up a JET connection and then SELECTing data from the sheet. However, for a real world...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
7
by: ddecoste | last post by:
I have a need to add a visual representation to some data in Access. I need to draw a matix of squares inside another square. I have all the data that I need in a record in Access. The data...
4
by: Brybot | last post by:
I have a form that i've split up into multiple asp:panels, each panel has a number of validators which work correctly. At on the last panel, i want to commit the data collected to a database. I...
16
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub...
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
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...
1
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
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.