Hi,
I have this main form frmOrders and subform sfrmOrders.
On the main form you can choose products from a listbox. If you click on a button next to the listbox, this product is added to a table called tblOrderlines. The products that have been choosen are seen in the subform. I get an error from Access when I choose the same product twice. There should be an error but I would like to give it myself by showing a simple message.
This is my code so far:
-------------------------------------------
Private Sub cmdChooseProduct_Click()
Dim con As Object
Dim stSql As String
Dim rs As Object
Dim stSql2 As String
Dim found As Integer
Set con = Application.CurrentProject.Connection
found = 0
stSql = "SELECT tblOrderlines.ProductId FROM tblOrderlines "
stSql = stSql & "WHERE tblOrderlines.OrderId =" & Me!OrderId
Set rs = CreateObject("ADODB.Recordset"
rs.Open stSql, con, 1 ' 1 = adOpenKeyset
While (Not (rs.EOF)) And found = 0
'cboId is the listbox
If rs![ProductId] = Me!cboId Then
Me.Undo
found = 1
End If
rs.MoveNext
Wend
If found = 1 Then
MsgBox "This product has already been choosen!"
Else
'Add product to the order
stSql2 = "INSERT INTO tblOrderlines (OrderId, ProductId)" & _
" VALUES (" & Me.OrderId & ", " & cboID & " "
Me.Refresh
con.Execute (stSql2)
sfrmOrders2.Requery
sfrmOrders2.SetFocus
End If
End Sub
-----------------------------------------------
The problem is that found always has the value 0. But I don't know why.
Could someone please help me?
Thanks in advance,
dj.
I have this main form frmOrders and subform sfrmOrders.
On the main form you can choose products from a listbox. If you click on a button next to the listbox, this product is added to a table called tblOrderlines. The products that have been choosen are seen in the subform. I get an error from Access when I choose the same product twice. There should be an error but I would like to give it myself by showing a simple message.
This is my code so far:
-------------------------------------------
Private Sub cmdChooseProduct_Click()
Dim con As Object
Dim stSql As String
Dim rs As Object
Dim stSql2 As String
Dim found As Integer
Set con = Application.CurrentProject.Connection
found = 0
stSql = "SELECT tblOrderlines.ProductId FROM tblOrderlines "
stSql = stSql & "WHERE tblOrderlines.OrderId =" & Me!OrderId
Set rs = CreateObject("ADODB.Recordset"
rs.Open stSql, con, 1 ' 1 = adOpenKeyset
While (Not (rs.EOF)) And found = 0
'cboId is the listbox
If rs![ProductId] = Me!cboId Then
Me.Undo
found = 1
End If
rs.MoveNext
Wend
If found = 1 Then
MsgBox "This product has already been choosen!"
Else
'Add product to the order
stSql2 = "INSERT INTO tblOrderlines (OrderId, ProductId)" & _
" VALUES (" & Me.OrderId & ", " & cboID & " "
Me.Refresh
con.Execute (stSql2)
sfrmOrders2.Requery
sfrmOrders2.SetFocus
End If
End Sub
-----------------------------------------------
The problem is that found always has the value 0. But I don't know why.
Could someone please help me?
Thanks in advance,
dj.