|
-
Mar 22nd, 2002, 12:15 PM
#1
Thread Starter
Junior Member
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
-
Mar 22nd, 2002, 12:35 PM
#2
VB Code:
Private Sub Text1_KeyPress(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
'also, in the change event put this code
'to prevent pasting in non numeric text
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
MsgBox "Only Numbers Are Allowed"
Text1.Text = ""
Exit Sub
End If
End Sub
-
Mar 23rd, 2002, 12:42 AM
#3
Thread Starter
Junior Member
THANKS, Hack
How about restricting inputs date (dd/mm/yyyy)?
-
Mar 23rd, 2002, 01:42 AM
#4
Thread Starter
Junior Member
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
-
Mar 23rd, 2002, 01:57 AM
#5
PowerPoster
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.
-
Mar 23rd, 2002, 02:21 AM
#6
Thread Starter
Junior Member
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
-
Mar 23rd, 2002, 02:33 AM
#7
PowerPoster
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.
-
Mar 23rd, 2002, 02:35 AM
#8
Hyperactive Member
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.
-
Mar 23rd, 2002, 03:01 AM
#9
Thread Starter
Junior Member
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.
-
Mar 23rd, 2002, 03:05 AM
#10
PowerPoster
Go on...
Simple ones I will post back quickly enough, for the tough ones I will post them back tomorrow....am at work
-
Mar 23rd, 2002, 03:22 AM
#11
Thread Starter
Junior Member
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
-
Mar 23rd, 2002, 03:38 AM
#12
Thread Starter
Junior Member
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
-
Mar 23rd, 2002, 05:12 AM
#13
PowerPoster
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 .
-
Mar 23rd, 2002, 06:52 AM
#14
Thread Starter
Junior Member
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.........
-
Mar 23rd, 2002, 06:56 AM
#15
Thread Starter
Junior Member
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.........
-
Mar 23rd, 2002, 07:09 AM
#16
PowerPoster
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...
-
Mar 23rd, 2002, 07:54 AM
#17
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|