Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert command on the fritz

Status
Not open for further replies.

smithbr

MIS
May 29, 2003
85
0
0
US
I have an add button on my windows form that sometimes works and sometimes does not. Sometimes if I quickly double click it will add the new record, other times it just takes on normal click, still other times it will not insert the new reord at all. Does anyone knwo why this might be happening?
Here is my code:
Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click

Dim strCommandText As String
Dim invnum As Integer
Dim intresult As Integer

invnum = txtinvnum.Text

If invnum > 20000 Then
strCommandText = "INSERT INTO InvoiceHistory (InvoiceDate,InvoiceNumber,LineSEQNo,CustomerNumber,ItemNumber,ProductLine,QuantityShipped,LastUnitPrice,DiscountP, PONumber) VALUES ("
strCommandText &= "'" & lbldatedisp.Text & "',"
strCommandText &= txtinvnum.Text & ","
strCommandText &= "'" & txtrefnum.Text & "',"
strCommandText &= "'" & lblcustdisp.Text & "',"
strCommandText &= "'" & cbotype.Items(cbotype.SelectedIndex) & "',"
strCommandText &= "'" & Mid$(cboprodln.Items(cboprodln.SelectedIndex), Len(cboprodln.Items(cboprodln.SelectedIndex)) - 3) & "',"
strCommandText &= "1," ' QuantityShipped
strCommandText &= txtdeduct.Text & ","
strCommandText &= "'0'," ' Discount
strCommandText &= "'" & txtrefnum.Text & "')"
Else
strCommandText = "INSERT INTO InvoiceHistory (InvoiceDate,InvoiceNumber,LineSEQNo,CustomerNumber,ItemNumber,ProductLine,QuantityShipped,LastUnitPrice,DiscountP, PONumber) VALUES ("
strCommandText &= "'" & lbltdatedisp.Text & "',"
strCommandText &= txtinvnum.Text & ","
strCommandText &= "'" & txtrefnum.Text & "',"
strCommandText &= "'" & lblcustdisp.Text & "',"
strCommandText &= "'" & cbotype.Items(cbotype.SelectedIndex) & "',"
strCommandText &= "'" & Mid$(cboprodln.Items(cboprodln.SelectedIndex), Len(cboprodln.Items(cboprodln.SelectedIndex)) - 3) & "',"
strCommandText &= "1," ' QuantityShipped
strCommandText &= txtdeduct.Text & ","
strCommandText &= "'0'," ' Discount
strCommandText &= "'" & txtrefnum.Text & "')"
End If

cmdaddinventory.CommandText = strCommandText
Try
OleDbConnection1.Open()

OleDbDataAdapter1.InsertCommand.CommandText = strCommandText
intresult = cmdaddinventory.ExecuteNonQuery

OleDbDataAdapter1.Update(DsInvoice1)
Catch Ex As OleDbException
System.Diagnostics.Debug.Write(Ex.Message)
End Try

OleDbConnection1.Close()


cmdfind_Click(sender, e)
End Sub
 

are you adding records on Click event event of a button???

what is cmdfind_Click(sender, e) doing???

This is how i would add records into my DB

Dim insertString As String 'Variable to hold the SQL statement.
Dim cmd As OleDbCommand 'Create an OleDbCommand object.
Dim insertResult As Integer 'Variable to hold result of ExecuteNonQuery method.

Try
'Open your connection
conn.Open()

'Initialize SQL string.
insertString = "INSERT INTO Deck (DeckName) " & _
" VALUES (" & """" & txtDeckName.Text & """" & ")"

'Initialize OleDbCommand object.
cmd = New OleDbCommand(insertString, conn)

'Send the CommandText to the connection, execute the Command.
insertResult = cmd.ExecuteNonQuery

Catch Excep As System.Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

Finally
'Close your connection.
conn.Close()
End Try


Tweak the above code as per your requirement.

Email: pankajmsm@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top