Link to home
Start Free TrialLog in
Avatar of Ned_Kelly
Ned_KellyFlag for Australia

asked on

MskEditBoxes are interferring with the writing of a random file

My application was writing to the file well until I changed to MaskEditBoxes for around half of the TextBoxes. I first had them with the AllowPrompt set to true, but I could not pull the values back from the file using the find command.
I changed them to false and was then able to retrieve all values for the Text and MaskEdit boxes using the find command.
Now I find that this stops the code writing to the file, I changed them back and I cant write to the file either way now.
I have commented out the MaskEdit code and the code then writes to the file as it should.
I'm somewhat stumpped, as I need to get this part working to deal with the two parts left, of which I have until Tuesday night to finish.
Thankyou for your help
Avatar of andyclap
andyclap
Flag of United Kingdom of Great Britain and Northern Ireland image

You're not giving much away here - any chance of some source code?
Avatar of Ned_Kelly

ASKER

Sorry,

Private Sub cmdEnter_Click()
    Const conMsg As String = "Account numbers range from 1 to 30."
    Const conBtns As Integer = vbOKOnly + vbInformation _
                + vbDefaultButton1 + vbApplicationModal
    Dim udtCredit As CreditStruc
    Dim strFileName As String ', i As Integer
    Dim intUserResponse As Integer '###, recordLength As Long
    Dim intAccNum As Integer ', x As Integer
   
        On Error GoTo ErrHandler

        intAccNum = Val(mskAccNum.Text)
        dlgCom.Filename = strFileName
        strFileName = "A:\Data.dat"
        '###recordLength = Len(udtCredit)


    '###If intAccNum < 0 And intAccNum > 30 Then
        '###intUserResponse = MsgBox(conMsg, conBtns, "Unlimited Credit")
    '###End If
        '###If mskAccNum.Text = "" Then
            '###MsgBox "You must enter an Account Number before saving this Record.", conBtns, "Validation Error"
        '###Else

            Open "A:\Data.dat" For Random As #1 Len = Len(udtCredit)
                udtCredit.intAccNum = Val(mskAccNum.Text)
                udtCredit.dteBirth = mskBirth.Text
                udtCredit.strLast = txtLast.Text
                udtCredit.strFirst = txtFirst.Text
                udtCredit.curBalance = Val(mskBalance.Text)
                udtCredit.strTelNum = mskTelNum.Text
                udtCredit.intIDNum = Val(txtIDNum.Text)
                lblDate1.Caption = Date
               
            '###For x = 1 To 30
                Put #1, intAccNum, udtCredit
            '###Next
            Close #1
        '###End If
 
   

    Call ClearFields
ErrHandler:

        mskAccNum.SelStart = 0
        mskAccNum.SelLength = Len(mskAccNum.Text)
        mskAccNum.SetFocus

End Sub

And this is the code for the Clearfields.

Private Sub ClearFields()
    'Clear the form for the next record
    Dim strMask As String
   
    mskAccNum.Mask = ""
    mskAccNum.Text = ""
    mskAccNum.Mask = "###"
    mskBirth.Mask = ""
    mskBirth.Text = ""
    mskBirth.Mask = "##/##/####"
    mskTelNum.Mask = ""
    mskTelNum.Text = ""
    mskTelNum.Mask = "(##) (########)"
    txtLast.Text = ""
    txtFirst.Text = ""
    txtIDNum.Text = ""
    mskBalance.Mask = ""
    mskBalance.Text = ""
    mskBalance.Mask = "#,###.##"

     
End Sub
Thankyou for your help
Ned
Sorry,

Private Sub cmdEnter_Click()
    Const conMsg As String = "Account numbers range from 1 to 30."
    Const conBtns As Integer = vbOKOnly + vbInformation _
                + vbDefaultButton1 + vbApplicationModal
    Dim udtCredit As CreditStruc
    Dim strFileName As String ', i As Integer
    Dim intUserResponse As Integer '###, recordLength As Long
    Dim intAccNum As Integer ', x As Integer
   
        On Error GoTo ErrHandler

        intAccNum = Val(mskAccNum.Text)
        dlgCom.Filename = strFileName
        strFileName = "A:\Data.dat"
        '###recordLength = Len(udtCredit)


    '###If intAccNum < 0 And intAccNum > 30 Then
        '###intUserResponse = MsgBox(conMsg, conBtns, "Unlimited Credit")
    '###End If
        '###If mskAccNum.Text = "" Then
            '###MsgBox "You must enter an Account Number before saving this Record.", conBtns, "Validation Error"
        '###Else

            Open "A:\Data.dat" For Random As #1 Len = Len(udtCredit)
                udtCredit.intAccNum = Val(mskAccNum.Text)
                udtCredit.dteBirth = mskBirth.Text
                udtCredit.strLast = txtLast.Text
                udtCredit.strFirst = txtFirst.Text
                udtCredit.curBalance = Val(mskBalance.Text)
                udtCredit.strTelNum = mskTelNum.Text
                udtCredit.intIDNum = Val(txtIDNum.Text)
                lblDate1.Caption = Date
               
            '###For x = 1 To 30
                Put #1, intAccNum, udtCredit
            '###Next
            Close #1
        '###End If
 
   

    Call ClearFields
ErrHandler:

        mskAccNum.SelStart = 0
        mskAccNum.SelLength = Len(mskAccNum.Text)
        mskAccNum.SetFocus

End Sub

And this is the code for the Clearfields.

Private Sub ClearFields()
    'Clear the form for the next record
    Dim strMask As String
   
    mskAccNum.Mask = ""
    mskAccNum.Text = ""
    mskAccNum.Mask = "###"
    mskBirth.Mask = ""
    mskBirth.Text = ""
    mskBirth.Mask = "##/##/####"
    mskTelNum.Mask = ""
    mskTelNum.Text = ""
    mskTelNum.Mask = "(##) (########)"
    txtLast.Text = ""
    txtFirst.Text = ""
    txtIDNum.Text = ""
    mskBalance.Mask = ""
    mskBalance.Text = ""
    mskBalance.Mask = "#,###.##"

     
End Sub

I can write to the file using textboxes but I need to validate the input,
so I have been using MaskeditBoxes.
Thankyou for your help
Ned
I have commented out what is not neccessary for writing to the file.
You set the masked edit box's IncludePromptChar to False, and check if it works.

Hope this helps,
Cheers.
Can you post the declaration of CreditStruc?
It could be that random access files expect each record to be the same length, if you're using variable length strings in the UDT you'll get into problems.
This is the module,

Option Explicit

    Type CreditStruc
        intAccNum As Integer
        strLast As String * 15
        strFirst As String * 15
        dteBirth As Date
        strTelNum As String * 15
        intIDNum As Integer
        lngBalance As Long
        lngDeposit As Long
        lngWithdrawal As Long
        dteDate As Date

   
    End Type

    Public lngBalance As Long
    Public lngDeposit As Long
    Public lngWithdrawal As Long
    Public dteBirth As Date
    Public strLast As String
    Public strFirst As String
    Public intIDNum As Integer
    Public strTelNum As String
    Public dteDate As Date
    Public intAccNum As Integer


I have been able to fix this problem, by adding code to the cmdEnter and the Clearfields.
Private Sub cmdEnter_Click()
    Const conMsg As String = "Account numbers range from 1 to 30."
    Const conBtns As Integer = vbOKOnly + vbInformation _
                + vbDefaultButton1 + vbApplicationModal
    Dim udtCredit As CreditStruc
    Dim strFileName As String
    Dim intUserResponse As Integer
    Dim intAccNum As Integer ', x As Integer
   
        On Error GoTo ErrHandler

        intAccNum = Val(mskAccNum.Text)
        dlgCom.Filename = strFileName
        strFileName = "A:\Data.dat"
       
        mskAccNum.PromptInclude = True
        mskBirth.PromptInclude = True
        mskTelNum.PromptInclude = True
        mskBalance.PromptInclude = True
   
   
    If intAccNum < 0 And intAccNum > 30 Then
        intUserResponse = MsgBox(conMsg, conBtns, "Unlimited Credit")
    End If
        If mskAccNum.Text = "" Then
            MsgBox "You must enter an Account Number before saving this Record.", conBtns, "Validation Error"
        Else

            Open "A:\Data.dat" For Random As #1 Len = Len(udtCredit)
                udtCredit.intAccNum = Val(mskAccNum.Text)
                udtCredit.dteBirth = mskBirth.Text
                udtCredit.strLast = txtLast.Text
                udtCredit.strFirst = txtFirst.Text
                udtCredit.lngBalance = Val(mskBalance.Text)
                udtCredit.strTelNum = mskTelNum.Text
                udtCredit.intIDNum = Val(txtIDNum.Text)
                lblDate1.Caption = Date
               
            '###For x = 1 To 30
                Put #1, intAccNum, udtCredit
            '###Next
            Close #1
        End If
 
   

    Call ClearFields
ErrHandler:

        mskAccNum.SelStart = 0
        mskAccNum.SelLength = Len(mskAccNum.Text)
        mskAccNum.SetFocus

End Sub


This is the Clearfields

Private Sub ClearFields()
    'Clear the form for the next record
    Dim strMask As String
   
    mskAccNum.Mask = ""
    mskAccNum.Text = ""
    mskAccNum.Mask = "###"
    mskAccNum.PromptInclude = True
    mskBirth.Mask = ""
    mskBirth.Text = ""
    mskBirth.Mask = "##/##/####"
    mskBirth.PromptInclude = True
    mskTelNum.Mask = ""
    mskTelNum.Text = ""
    mskTelNum.Mask = "(##) (########)"
    mskTelNum.PromptInclude = True
    txtLast.Text = ""
    txtFirst.Text = ""
    txtIDNum.Text = ""
    mskBalance.Mask = ""
    mskBalance.Text = ""
    mskBalance.Mask = "#,###.##"
    mskBalance.PromptInclude = True
   

     
End Sub


For some reason it wont find when the PromptInclude is set to True, and it wont write when it is set to False.
The find code is below.

Private Sub cmdFind_Click()
    Const conMsg As String = "Account numbers range from 1 to 30."
    Const conBtns As Integer = vbOKOnly + vbInformation _
                + vbDefaultButton1 + vbApplicationModal
    Dim udtCredit As CreditStruc, intAccNum As Integer, intUserResponse As Integer
    Dim intIDNum As Integer, intIndex As Integer
   
    On Error Resume Next

    'Read record from file
    Open "A:\Data.dat" For Random As #1 Len = Len(udtCredit)

    intAccNum = Val(mskAccNum.Text)
   
        If mskAccNum.Text <> "" Then
            If intAccNum >= 0 And intAccNum <= 30 Then
                Get #1, intAccNum, udtCredit
                    txtLast.Text = Trim(udtCredit.strLast)
                    txtFirst.Text = Trim(udtCredit.strFirst)
                    mskBirth.PromptInclude = False
                    mskBirth.Text = Trim(udtCredit.dteBirth)
                    mskTelNum.PromptInclude = False
                    mskTelNum.Text = Trim(udtCredit.strTelNum)
                    txtIDNum.Text = Trim(udtCredit.intIDNum)
                    mskBalance.PromptInclude = False
                    mskBalance.Text = Trim(udtCredit.lngBalance)
                    lblDate1.Caption = Date
                   
            Else
                intUserResponse = MsgBox(conMsg, conBtns, "Unlimited Credit.")
            End If
        ElseIf txtLast.Text <> "" Then
            For intIndex = 1 To 30
                Get #1, intIndex, udtCredit
                If txtLast.Text = Trim(udtCredit.strLast) Then
                    mskAccNum.PromptInclude = False
                    mskAccNum.Text = Trim(udtCredit.intAccNum)
                    txtLast.Text = Trim(udtCredit.strLast)
                    txtFirst.Text = Trim(udtCredit.strFirst)
                    mskBirth.PromptInclude = False
                    mskBirth.Text = Trim(udtCredit.dteBirth)
                    mskTelNum.PromptInclude = False
                    mskTelNum.Text = Trim(udtCredit.strTelNum)
                    txtIDNum.Text = Trim(udtCredit.intIDNum)
                    mskBalance.PromptInclude = False
                    mskBalance.Text = Trim(udtCredit.lngBalance)
                    lblDate1.Caption = Date
                End If
            Next
        End If

       
        mskAccNum.SelStart = 0
        mskAccNum.SelLength = Len(mskAccNum.Text)
        mskAccNum.SetFocus
End Sub

Good to hear from you
Thankyou Ned
Hi folks, seeing as I have sort of fixed it myself, if you could help me fix the problem of when I type an account number in and press find it brings up the record but displays the mskBalance as a single figure instead of the eight spaces that it should. The birth date misses out the zeros and when I change the telephone number and press enter again it does nothing.
I think I need to add something to the code at the end of the cmdFind.
So there are a few that need fixing, I will give the points for any one, two or three that can be fixed.
I know its diverging from the original question, but what am I supposed to do in this situation.
Hope to hear
Ned
ASKER CERTIFIED SOLUTION
Avatar of Valliappan AN
Valliappan AN
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Valli An, though its a bit late for me now, as I have to hand it in tonight, and its worth 80% of the mark.
He (the Instructor) gave us the ultimatum that it has to work or we fail, so I'm now preparing myself to redo the module as soon as I can, though I haven't give up yet.
Regards Ned

Ps. I will keep your comments for future reference.
You can try on the following, if it works for you, hope you should find using them easier:

1) Use Formatted text, wherever you wish to get the data from Masked Edit control.
2) Use Clip text, to get it without formatting.
3) Use Format property for changing the display of text in Masked Edit property.

All the best.
Thanks Valli An, very appreciated.
Ned
Ned,

Thank you. How about your submitting? you did it?

Cheers.
I got a reprieve from my instructor, there is a god after all.
I had also misunderstood him somewhat, instead of creating a new file each time I was supposed to create a new record each time, this make s the code somewhat easier.
With the reprieve and the change I should be able to get it done by Sunday and email it to him.
Phew...
Thanks again.
Ned
Thanks for your guidance in this matter, I have changed the mskBalance box to a text box and am now in the process of coding it to hopefully do what I want.
Ned
Thats good. Text box is easier to handle.

As I said earlier, you need to validate for numeric keys in keypress event, or a more advanced one, in change event, but that is a difficult one. Anyhow, if you need it, let me know.

Then in validate event, you could format the text in the format you need, with say, the Format function.

In gotfocus event, may be you could put some code like:

Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text) '(or) Text1.Maxlength

or you could alter it as you need.

Do you need, any suggestions, let me know. You can also find these codes/examples, in many web sites.

Thanks for the points.

Cheers.
Hi Valli An, I have put the following code into the keypress but it still takes letters

Private Sub txtBal_KeyPress(KeyAscii As Integer)
    If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
        txtBal.Text = txtBal.Text & Chr$(KeyAscii)

    End If
   
End Sub

I have tried to validate it  with the code below.

Private Sub txtBal_Validate(Cancel As Boolean)
    Dim KeyAscii() As Integer
    If txtBal.Text >= vbKey0 And txtBal.Text <= vbKey9 Then
        txtBal.Text = KeyAscii(vbKey0 - vbKey9)
    Else
        MsgBox "You can only enter numbers in the balance box."
    End If
       
End Sub

Any help would be appreciated,
thankyou
Ned.