Solved

Masked Edit from VB to Access

Posted on 1998-06-09
4
371 Views
Last Modified: 2010-05-03
When I have a masked edit box in VB as ##/##/## (Date) and a short date format in my input mask in Access, I can't retrieve the data correctly from Access if the month contains a zero as the first digit (01/11/98) will be 11/19/8 in VB masked edit box. How do I correct it?
0
Comment
Question by:paulca
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
swilt earned 20 total points
Comment Utility
In Access
    Format : Short Date
    My Input Mask is blank

In Visual Basic (Masked Edit)
    Unbound data control
    Mask = "##/##/##"



Option Explicit

Private Sub Data1_Reposition()
    Dim sMask As String, sDate As String
   
    If IsNull(Data1.Recordset.MyDate) Then
        sMask = MaskEdBox1.Mask
        MaskEdBox1.Mask = ""
        MaskEdBox1.Text = ""
        MaskEdBox1.Mask = sMask
    Else
        sDate = Format$(Data1.Recordset.MyDate, "short date")
        If Len(sDate) = 10 Then 'chop the century
            sDate = Left$(sDate, 6) & Right$(sDate, 2)
        End If
        MaskEdBox1.Text = ReplaceChar(sDate, ".", "/")
    End If
End Sub

Private Function ReplaceChar(ByVal sInp As String, _
                             ByVal sChar As String, _
                             ByVal sRepl As String) As String
    Dim nPos As Integer
   
    nPos = InStr(sInp, sChar)
    While nPos > 0
        Mid$(sInp, nPos, 1) = sRepl
        nPos = InStr(sInp, sChar)
    Wend
    ReplaceChar = sInp
End Function


0
 
LVL 2

Expert Comment

by:swilt
Comment Utility
I forgot to update the date

Private Sub Data1_Validate(Action As Integer, Save As Integer)
    If InStr(MaskEdBox1.Text, "_") = 0 Then
        Data1.Recordset.Edit
        Data1.Recordset!MyDate = MaskEdBox1.Text
        Data1.Recordset.Update
    Else
        MsgBox "Invalid Date"
    End If
End Sub

0
 
LVL 2

Expert Comment

by:swilt
Comment Utility
A bit more validation - should allow empty date entry and not move on if the date is invalid

Private Sub Data1_Validate(Action As Integer, Save As Integer)
    If MaskEdBox1.Text = ReplaceChar(MaskEdBox1.Mask, "#", "_") Then
        Data1.Recordset.Edit
        Data1.Recordset!MyDate = Null
        Data1.Recordset.Update
    Else
        If InStr(MaskEdBox1.Text, "_") = 0 And IsDate(MaskEdBox1.Text) Then
            Data1.Recordset.Edit
            Data1.Recordset!MyDate = MaskEdBox1.Text
            Data1.Recordset.Update
        Else
            MsgBox "Invalid Date"
            Action = vbDataActionCancel
        End If
    End If
End Sub
0
 
LVL 1

Expert Comment

by:Celia
Comment Utility
Hi Swilt,

I have two masked edit box in VB6 as ##/##/####(Short Date) and ##:##(Short Time).My only problem is that I can't display my 1st MsgBox.

Coding(Date) as as below:

Private Sub txtAdmdate_Validate(Cancel As Boolean)

If txtAdmdate.Text = ReplaceChar(txtAdmdate.Mask, "_") Then
    '1st Msgbox
    MsgBox "Blank date"
Else
    If InStr(txtAdmdate.Text, "_") = 0 And IsDate(txtAdmdate.Text) Then
    '2nd MsgBox
    MsgBox "Valid Date"
Else
    '3rd MsgBox
    MsgBox "Invalid Date"
End If

End Sub


Compile Error : Argument Not Optional.(ReplaceChar)

I appreciated a lot if you can help me.

With Thx,
Celia


0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now