Results 1 to 17 of 17

Thread: Restrict Inputs

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21

    Restrict Inputs

    how to restrict the user to input number only to a textbox?

    i've tried to use masked boxes, but when i link them to the ADO control, error ocurs: unable to bind the field or data member

    the problem comes from the mask property..

    for example, i want to make a text box to accept weight(kg) from users, so i set the mask - 999.9

    is there a simple way to do the same thing?

    thanks in advance
    Frank

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
    3.     MsgBox "Only numbers are allowed for this entries"
    4.     KeyAscii = 0
    5.     Exit Sub
    6. End If
    7. End Sub
    8.  
    9. 'also, in the change event put this code
    10. 'to prevent pasting in non numeric text
    11. Private Sub Text1_Change()
    12. If Not IsNumeric(Text1.Text) Then
    13.    MsgBox "Only Numbers Are Allowed"
    14.    Text1.Text = ""
    15.    Exit Sub
    16. End If
    17. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21
    THANKS, Hack

    How about restricting inputs date (dd/mm/yyyy)?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21
    Hack, I have tried, but it doesn't work, the MsgBox "Only Numbers Are Allowed" appears when i run the program, and i need to terminate the program, do i have any mistakes in the code, can you help me?
    also, as there are 8 textbox, tx_pr(0-7).



    Private Sub tx_Pr_GotFocus(index As Integer)
    tx_Pr(index).SelStart = 0
    tx_Pr(index).SelLength = Len(tx_Pr(index).Text)
    End Sub

    ------------------------------------------------------------------------------------
    Private Sub tx_Pr_KeyDown(index As Integer, KeyCode As Integer, Shift As Integer)
    If index <> 3 Then
    If KeyCode = vbKeyReturn Then
    SendKeys "{tab}"
    End If
    End If
    End Sub

    ------------------------------------------------------------------------------------
    Private Sub tx_pr_KeyPress(index As Integer, KeyAscii As Integer)
    If index > 1 And index < 8 Then
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
    MsgBox "Only numbers are allowed for this entries"
    KeyAscii = 0
    Exit Sub
    End If
    End If
    End Sub

    ------------------------------------------------------------------------------------
    Private Sub tx_pr_Change(index As Integer)
    If index > 1 And index < 8 Then
    If Not IsNumeric(tx_Pr(i).Text) Then
    MsgBox "Only Numbers Are Allowed"
    tx_Pr(i).Text = ""
    Exit Sub
    End If
    End If
    End Sub

    Thanks A Lot
    Frank

  5. #5
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    Hacks code is correct.

    For more than one textbox use:

    Code:
    Private Sub txtNumeric_Change(Index As Integer)
        If Not IsNumeric(txtNumeric(Index)) Then
            MsgBox "Only numbers are allowed"
            txtNumeric(Index).Text = ""
        End If
    End Sub
    
    Private Sub txtNumeric_KeyPress(Index As Integer, KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
        MsgBox "Only numbers are allowed for this entries"
        KeyAscii = 0
        Exit Sub
    End If
    End Sub
    Where txtNumeric is the name of the textbox array.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21

    Hi, Thanks

    But the MsgBox still appears when i run the program, it appears 6 times before the form appears.....
    Also, there are many many problems....
    I have been working for days, but i still cannot fix them.....
    can anybody help.............

    Private Sub cbPr_LostFocus(Index As Integer)
    'Check whether to add new category
    Dim i As Long
    Dim Flag As Boolean

    Select Case Index
    Case 1
    Flag = False
    If cbPr(1).Text <> "" Then
    'Prevent MsgBox appear when cbPr(1) = ""
    For i = 0 To cbPr(1).ListCount - 1
    If cbPr(1).List(i) = cbPr(1).Text Then
    'if exists
    Flag = True
    i = cbPr(1).ListCount - 1
    'end the for loop
    End If
    Next
    Else: Flag = True
    End If
    If Flag = False Then
    If MsgBox("Add the Category " & cbPr(1).Text & "?", vbYesNo + vbQuestion, "Add Category") = vbNo Then
    cbPr(1).Text = ""
    'confirm
    'need to go back to the category text box
    End If
    End If
    End Select

    End Sub

    Private Sub Form_Load()
    On Error Resume Next
    Dim Pr_No As String
    Dim i As Long

    cbPr(0).Text = ""
    cbPr(1).Text = ""
    cbPr(0).Clear
    cbPr(1).Clear

    'Add to list from 001 to 999, exept those already exists
    If ADO_Pr.Recordset.RecordCount <> 0 Then
    ADO_Pr.Recordset.MoveFirst
    For i = 1 To 999
    If i <> 250 Then
    '250 is an exeption, it is 250a&b
    If Val(ADO_Pr.Recordset(0)) <> i Then
    Select Case Len(Str(i))
    Case 2
    Pr_No = "00" & Right(Str(i), 1)
    Case 3
    Pr_No = "0" & Right(Str(i), 2)
    Case 4
    Pr_No = Right(Str(i), 3)
    End Select
    cbPr(0).AddItem Pr_No
    Else
    ADO_Pr.Recordset.MoveNext
    End If
    Else: ADO_Pr.Recordset.MoveNext
    End If
    Next
    Else
    'If nothing in DB
    For i = 1 To 999
    cbPr(0).AddItem i
    Next
    End If

    'Add to List, Category
    ADO_Pr.Recordset.MoveFirst
    For i = 1 To ADO_Pr.Recordset.RecordCount
    Flag = 0
    If cbPr(1).ListCount <> 0 Then
    For j = 0 To cbPr(1).ListCount - 1
    If cbPr(1).List(j) = ADO_Pr.Recordset(3) Then
    Flag = 1
    End If
    Next
    End If

    If Flag = 0 Then
    cbPr(1).AddItem ADO_Pr.Recordset(3) & ""
    End If
    ADO_Pr.Recordset.MoveNext
    Next

    ADO_Pr.Recordset.AddNew

    End Sub

    Private Sub CmPr_Click(Index As Integer)

    Select Case Index
    Case 0
    ADO_Pr.Recordset(0) = cbPr(0).Text
    ADO_Pr.Recordset(3) = cbPr(1).Text
    ADO_Pr.Recordset.Update
    First.Show
    Unload Me
    Case 1
    ADO_Pr.Recordset(0) = cbPr(0).Text
    ADO_Pr.Recordset(3) = cbPr(1).Text
    ADO_Pr.Recordset.Update
    Form_Load
    Case 2
    If MsgBox("Cancel?", vbYesNo + vbExclamation, "Cancel") = vbYes Then
    ADO_Pr.Recordset.CancelUpdate
    First.Show
    Unload Me
    End If
    Case 3
    NPD.Show 1
    End Select

    End Sub


    Private Sub tx_Pr_GotFocus(Index As Integer)
    tx_Pr(Index).SelStart = 0
    tx_Pr(Index).SelLength = Len(tx_Pr(Index).Text)
    'need to change IME
    End Sub

    Private Sub tx_Pr_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
    If Index <> 7 Then
    If KeyCode = vbKeyReturn Then
    SendKeys "{tab}"
    End If
    End If
    End Sub

    Private Sub tx_pr_KeyPress(Index As Integer, KeyAscii As Integer)

    If Index > 1 And Index < 8 Then
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 Then
    Beep
    MsgBox "Only numbers are allowed for this entries"
    KeyAscii = 0
    Exit Sub
    End If
    End If
    End Sub

    Private Sub tx_pr_Change(Index As Integer)
    If Index > 1 And Index < 8 Then
    If Not IsNumeric(tx_Pr(Index)) Then
    Beep
    MsgBox "Only Numbers Are Allowed"
    tx_Pr(Index).Text = ""
    End If
    End If
    End Sub

  7. #7
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    First of all I would like to suggest that u use code blocks while pasting your code.It becomes easier to read.

    Originally posted by Frank716
    But the MsgBox still appears when i run the program, it appears 6 times before the form appears.....
    Also, there are many many problems....
    I have been working for days, but i still cannot fix them.....
    can anybody help.............
    about the messages just add this:

    Code:
    Private Sub tx_pr_Change(Index As Integer) 
    if len(tx_pr(Index)) > 0 then 
    If Index > 1 And Index < 8 Then 
    If Not IsNumeric(tx_Pr(Index)) Then 
    Beep 
    MsgBox "Only Numbers Are Allowed" 
    tx_Pr(Index).Text = "" 
    End If 
    End If 
    end if
    End Sub
    This will help you get rid of the "onloading" message boxes.

    About the other probs. u can post them back one - by - one.


  8. #8
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463
    it seems to me that ur using ADODC, well f i'm right u might had accidentally bounded the textboxes to the ADODC ur using. and if the field on the source tbl does not contain numbers, chances are the msgbox will appear.

    the textbox when bounded to a datacontrol will have its change event invoke evrytime the adodc is browsed and the first time it is loaded. what i mean by this is dat when a form loads the code in the change event of a textbox fires up as data is loaded on its text property.
    live, code and die...

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21

    Thank You

    veryjohn, it works, Thanks. do you have time?
    can i ask you some more questions? since i got headache about the program, i used to code programs using foxpro, i think it is easier to control databases, however it is difficult to make a user-friendly interface. In fact i code the program since october last year, but till now, the program is not functioning, and the deadline is coming soon......................

    dRAMer, in the database(Access), they are all numbers, but thanks for your information.

  10. #10
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089
    Go on...

    Simple ones I will post back quickly enough, for the tough ones I will post them back tomorrow....am at work

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21

    Thank Very Much, veryjonny

    how can i point to a textbox, i used LostFocus to check whether to add new category, it is a combo box, i have listed the existing category. but when a user wants to add new one, a confirmation is need, i he/she agrees, then proceed, if not, point back to the category textbox, but i don't know how to do it.

    Private Sub cbPr_LostFocus(Index As Integer)
    'Check whether to add new category
    Dim i As Long
    Dim Flag As Boolean

    Select Case Index
    Case 1
    Flag = False
    If cbPr(1).Text <> "" Then
    'Prevent MsgBox appear when cbPr(1) = ""
    For i = 0 To cbPr(1).ListCount - 1
    If cbPr(1).List(i) = cbPr(1).Text Then
    'if exists
    Flag = True
    i = cbPr(1).ListCount - 1
    'end the for loop
    End If
    Next
    Else: Flag = True
    End If
    If Flag = False Then
    If MsgBox("Add the Category " & cbPr(1).Text & "?", vbYesNo + vbQuestion, "Add Category") = vbNo Then
    cbPr(1).Text = ""
    'confirm
    'need to go back to the category text box
    End If
    End If
    End Select

    End Sub

    Thank You
    Frank

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21

    Sorry For So Many Questions....

    Here the Product ID - ADO_Pr.Recordset(0) will be from 001 to 999, the combo box, cbPr(0) lists out IDs which is not existing. using the following code, I succeeded, but when i append a new record, let say, 014, the combo box will not eliminate 014, but I cannot find the reason.

    Here is the code....
    Private Sub Form_Load()
    On Error Resume Next
    Dim Pr_No As String
    Dim i As Long

    cbPr(0).Text = ""
    cbPr(1).Text = ""
    cbPr(0).Clear
    cbPr(1).Clear

    'Add to list from 001 to 999, exept those already exists
    If ADO_Pr.Recordset.RecordCount <> 0 Then
    ADO_Pr.Recordset.MoveFirst
    For i = 1 To 999
    If i <> 250 Then
    '250 is an exeption, it is 250a&b
    If Val(ADO_Pr.Recordset(0)) <> i Then
    Select Case Len(Str(i))
    Case 2
    Pr_No = "00" & Right(Str(i), 1)
    Case 3
    Pr_No = "0" & Right(Str(i), 2)
    Case 4
    Pr_No = Right(Str(i), 3)
    End Select
    cbPr(0).AddItem Pr_No
    Else
    ADO_Pr.Recordset.MoveNext
    End If
    Else: ADO_Pr.Recordset.MoveNext
    End If
    Next
    Else
    'If nothing in DB
    For i = 1 To 999
    cbPr(0).AddItem i
    Next
    End If


    'not related'
    'Add to List, Category
    ADO_Pr.Recordset.MoveFirst
    For i = 1 To ADO_Pr.Recordset.RecordCount
    Flag = 0
    If cbPr(1).ListCount <> 0 Then
    For j = 0 To cbPr(1).ListCount - 1
    If cbPr(1).List(j) = ADO_Pr.Recordset(3) Then
    Flag = 1
    End If
    Next
    End If

    If Flag = 0 Then
    cbPr(1).AddItem ADO_Pr.Recordset(3) & ""
    End If
    ADO_Pr.Recordset.MoveNext
    Next


    ADO_Pr.Recordset.AddNew

    End Sub

    'here is the code of the command box'
    Private Sub CmPr_Click(Index As Integer)

    Select Case Index
    Case 0

    ADO_Pr.Recordset(0) = cbPr(0).Text
    ADO_Pr.Recordset(3) = cbPr(1).Text
    ADO_Pr.Recordset.Update
    First.Show
    Unload Me
    Case 1
    ADO_Pr.Recordset(0) = cbPr(0).Text
    ADO_Pr.Recordset(3) = cbPr(1).Text
    ADO_Pr.Recordset.Update
    Form_Load
    Case 2
    If MsgBox("Cancel?", vbYesNo + vbExclamation, "Cancel") = vbYes Then
    ADO_Pr.Recordset.CancelUpdate
    First.Show
    Unload Me
    End If
    Case 3
    NPD.Show 1
    End Select

    End Sub

    Frank

  13. #13
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    Sorry , I went out for lunch.


    I didnt quite understand what you are trying to do.

    I understand this that on the form onload event - you try to fill up a combobox with values from the database however there is a special case of 250 which has two values.

    Now after this u want to allow the user:

    a) add a new record?
    b) edit a previous record?

    This I am clear with.

    If you want to add a new record you can display him another form where he can add the new record.

    I guess you are trying to do something like the moment he selects a record from the combobox youdisplay him the data.

    Tell me without posting the code first - what you are trying to achieve .

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21
    In fact i would like to use the same form for Adding New records and Editing/Modifying Existing Records.

    But for the time being, I would like to concentrate on Adding new records first.

    For the combo box, I would like to show the Product ID in it, and these IDs should not exist in the database. Since I'm not very good in English, so I find it difficult to describe the 250 case (so the tone i used in the messages may sounds strange, if some are impolite, I'm sorry, but I really don't mean that, anyway, Thanks a lot). So I would like to use an example.

    In the database, the field Product ID - Pr_ID, Contains
    001, 002, 003, 006, 249, 250a&b, 251,350

    So I would like to show the IDs in the list - 004, 005, 007 to 248, 252 to 349, 351 to 999. 250a&b is an exception which can not be modified to 250.

    the Problem is that after i add a new record - 005, the 005 still exist in the combo box.........

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21
    There are 3 tables in the access database- Product, Customer, PO(Purchase Order), PO_Tmp.

    The program will provide new, edit, print and search for each of the 1st 3 tables, the last one is using to store the temporary records of PO.

    I think that the program is not difficult but when i started to construct it, problems flood.........

  16. #16
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    hi

    the Problem is that after i add a new record - 005, the 005 still exist in the combo box.........
    Kindly tell me what u mean by this...


    Its nearly nwo my leaving time, We will solve your prob on monday.

    Post back and I will develop a script for you to help...

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Hong Kong
    Posts
    21
    may be i would like to stop the new form, now i'm working in the search part, after i filter some of the records, then i use ADO_Fil.Refresh, but error occurs :

    Run-time error '2147217900 (80040e14)':

    Method 'Refresh' of object 'Iadodc' failed..................

    I don't know what is wrong.........

    can you help?

    Thanks
    Frank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width