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
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