so this is my code for inserting files and what i need to autoincrement the codes but the thing is i cant use the id since it has so many department in just one table for example
OP-BLDG-16-0001
LP-LAND-16-0001
codename.Text = "" & codedepartment.Text & "-" & code1.Text & "-" & Now.Date.ToString("yy") & "-" & "00" & ""
So thats what i did to get first 3 info and i just have to set it into autoincrement
Try
dataB = "INSERT into assetpp ( [codes],[codedepartmentname]) VALUES ( '" & codename.Text & "','" & codedepartment.Text & "')"
ConnDB()
cmd = New OleDbCommand(dataB, conn)
Dim i As Integer
i = cmd.ExecuteNonQuery
If i > 0 Then
MsgBox("Add Succesfully", MsgBoxStyle.Information, "Confirmation")
tableassetpp()
Else
MsgBox("Failed to add Data", MsgBoxStyle.Information, "Alert")
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
conn.Close()
End Try
Static cnt As Integer
cnt = cnt + 1
codename.Text = "" & codedepartment.Text & "-" & code1.Text & "-" & Now.Date.ToString("yy") & "-" & Format(cnt, "0000") & ""
Related
Following are my codes Pls Help me
I found this error on updating the Table
Private Sub UpdateInventory()
Dim connectionString As String = ConfigurationManager.ConnectionStrings("MainConString").ConnectionString
Dim con As New SqlConnection(connectionString)
Dim Query1 As String
Try
Dim Result As DialogResult
Result = MessageBox.Show(" Update this Record ?", "Save ! ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
For J As Integer = 0 To DataGridStock.Rows.Count - 1 Step +1
Query1 = "Update Tempstock Set Type='" & LabelTrancode.Text & "', Prefix='" & Lblprefix.Text & "', Srl='" & TextINVNo.Text & "',
DOcDate='" & TextBoxdocdate.Text & "',Sno='" & DataGridStock.Rows(J).Cells(0).Value & "', Hsncode='" & DataGridStock.Rows(J).Cells(4).Value & "',
Branch='" & LBLBranchcode.Text & "',Taxcode='" & DataGridStock.Rows(J).Cells(5).Value & "',Code='" & DataGridStock.Rows(J).Cells(2).Value & "',
Qty='" + (DataGridStock.Rows(J).Cells(6).Value) + "',Rate='" + Int(DataGridStock.Rows(J).Cells(8).Value) + "',Unit='" & DataGridStock.Rows(J).Cells(7).Value & "'"
Dim query = String.Concat(Query1, ";")
con.Open()
cmd = New SqlCommand(query, con)
reader = cmd.ExecuteReader
reader.Close()
con.Close()
Next
MessageBox.Show(" Stock Updated")
End If
Catch ex As Exception
End Try
End Sub
I can execute an Access SQL INSERT statement without error, but when I attempt an UPDATE with the code below, it throws the error:
"Syntax error in string in query expression".
Why does that happen and how can I fix it?
Here is my code..
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
If txtName.Text = Nothing Or dtpDateBirth.Text = Nothing Or txtPhoneNumber.Text = Nothing Or txtEmail.Text = Nothing Or txtID.Text = Nothing Or mtbNPWP.Text = Nothing Or rtbAddress.Text = Nothing Or txtVillage.Text = Nothing Or txtSubDistrict.Text = Nothing Or txtCity.Text = Nothing Or txtPostalCode.Text = Nothing Or txtEmergencyName.Text = Nothing Or txtEmergencyNumber.Text = Nothing Or txtEmergencyEmail.Text = Nothing Then
MsgBox("No data has been update", MsgBoxStyle.Information, "No data edit")
Exit Sub
Else
Call koneksi()
cmd = New OleDb.OleDbCommand("update t_customer set name='" & txtName.Text & "', date_birth='" & dtpDateBirth.Text & "', phone_number='" & txtPhoneNumber.Text & "', email='" & txtEmail.Text & "', id='" & txtID.Text & "', npwp='" & mtbNPWP.Text & "', address='" & rtbAddress.Text & "', village='" & txtVillage.Text & "', sub_district='" & txtSubDistrict.Text & "', city='" & txtCity.Text & "', postal_code='" & txtPostalCode.Text & "', gender='" & gen & "', emergency_name='" & txtEmergencyName.Text & "', phone_emergency='" & txtPhoneNumber.Text & "', email_emergency='" & txtEmergencyEmail.Text & "' where [codeCust]='" & txtIDCust.Text, conn)
cmd.ExecuteNonQuery()
MsgBox("Update data success!", MsgBoxStyle.Information, "Update")
End If
Call disabled()
Call showData()
End Sub
As HansUp has suggested with the link you should be using parameters. Also you are not disposing the command. Also AFAIK (I thought) you needed the open the connection before you can ExecuteNonQuery. As for the Data Mismatch youre experiencing, I think it's because you have a ' in the where clause that shouldnt be there, assuming that the ID is an INT.
Using OledbConn As New OleDbConnection("Conn String")
OledbConn.Open()
Using t_customerCMD As New OleDbCommand("UPDATE t_customer SET [Col1]=#Col1,[Col2]=#Col2 WHERE [codeCust]=#codeCust", OledbConn)
With t_customerCMD
.Parameters.Add("#Col1", SqlDbType.VarChar).Value = TextBox1.Text
.Parameters.Add("#Col2", SqlDbType.VarChar).Value = TextBox2.Text
.Parameters.Add("#codeCust", SqlDbType.Int).Value = CInt(txtIDCust.Text) 'Assumes ID is integer
.ExecuteNonQuery()
End With
End Using
OledbConn.Close()
End Using
End Sub
Thank you for everyone who helped me. I thank you very much. This problem has been resolved.
this is my completion code, which I can finally do to update the database.
Sub updateData()
Call gender()
If String.IsNullOrEmpty(txtName.Text) OrElse String.IsNullOrEmpty(dtpDateBirth.Text) OrElse String.IsNullOrEmpty(txtPhoneNumber.Text) OrElse String.IsNullOrEmpty(txtEmail.Text) OrElse String.IsNullOrEmpty(txtID.Text) OrElse String.IsNullOrEmpty(mtbNPWP.Text) OrElse String.IsNullOrEmpty(rtbAddress.Text) OrElse String.IsNullOrEmpty(txtVillage.Text) OrElse String.IsNullOrEmpty(txtSubDistrict.Text) OrElse String.IsNullOrEmpty(txtCity.Text) OrElse String.IsNullOrEmpty(txtPostalCode.Text) OrElse String.IsNullOrEmpty(txtEmergencyName.Text) OrElse String.IsNullOrEmpty(txtEmergencyNumber.Text) OrElse String.IsNullOrEmpty(txtEmergencyEmail.Text) Then
MsgBox("No data has been update", MsgBoxStyle.Information, "No data edit")
Exit Sub
Else
Try
Call koneksi()
Using cmd = New OleDb.OleDbCommand("update t_customer set name='" & txtName.Text & "', date_birth='" & dtpDateBirth.Text & "', phone_number='" & txtPhoneNumber.Text & "', email='" & txtEmail.Text & "', id='" & txtID.Text & "', npwp='" & mtbNPWP.Text & "', address='" & rtbAddress.Text & "', village='" & txtVillage.Text & "', sub_district='" & txtSubDistrict.Text & "', city='" & txtCity.Text & "', postal_code='" & txtPostalCode.Text & "', gender='" & gen & "', emergency_name='" & txtEmergencyName.Text & "', phone_emergency='" & txtPhoneNumber.Text & "', email_emergency='" & txtEmergencyEmail.Text & "' where [codeCust]=#codeCust", conn)
cmd.Parameters.Add("codeCust", CType(txtIDCust.Text, String))
cmd.ExecuteNonQuery()
conn.Close()
MsgBox("Update data success!", MsgBoxStyle.Information, "Update")
End Using
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Because I use the radio button for gender options, I added this so that gender can be changed.
Private Sub gender()
Call koneksi()
Dim str As String
str = "Insert into t_customer([gender]) Values (?)"
Dim cmd As OleDbCommand = New OleDbCommand(str, conn)
If rdnMale.Checked = True Then
gen = rdnMale.Text
cmd.Parameters.Add(New OleDbParameter("gender", CType(gen, String)))
Else
gen = rdnFemale.Text
End If
End Sub
I am using a select statement to retrieve records from an ms access table between two dates and another condition to check for customers name
I get the error "No value given for one or more required parameters"
The code is as below
Try
If Conn.State = ConnectionState.Closed Then
Conn.Open()
End If
Dim dtDate1 As DateTime = DateTime.Parse(dtpDateFrom.Text)
Dim dtDate2 As DateTime = DateTime.Parse(dtpDateTo.Text)
''''SQL_PaymentsP = "SELECT InvoiceID,CustomerName,InvoiceDate,InvoiceAmount,PaymentDesc,PaidAmount,DatePaid,CurrentBalance FROM Payments WHERE [DatePaid] BETWEEN #" & dtDate1.ToString("MM/dd/yyyy") & "# AND #" & dtDate2.ToString("MM/dd/yyyy") & "# " & "OR [CustomerName] = " & txtCustomer.Text & ""
SQL_PaymentsP = "SELECT Payments.PaymentID, Payments.InvoiceID, Payments.CustomerName, Payments.InvoiceDate, Payments.InvoiceAmount, Payments.PaymentDesc, Payments.PaidAmount, Payments.DatePaid, Payments.CurrentBalance, Payments.Status FROM Payments WHERE Payments.DatePaid Between #" & dtDate1.ToString("MM/dd/yyyy") & "# And #" & dtDate2.ToString("MM/dd/yyyy") & "# " & " OR " & "Payments.CustomerName =" & txtCustomer.Text & ""
' SQLInvoicesP = "Select DateInvoice,IDInvoices,InvoiceAmount,CustomerName,Mode from Invoices where DateInvoice between #" & dtDate1.ToString("MM/dd/yyyy") & "# and #" & dtDate2.ToString("MM/dd/yyyy") & "#"
DataSet_PaymentsP.Clear()
Dim DataAdapter_PaymentsP As New OleDbDataAdapter(SQL_PaymentsP, Conn)
DataAdapter_PaymentsP.Fill(DataSet_PaymentsP, "Payments")
Conn.Close()
Application.DoEvents()
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
ConInfo.ConnectionInfo.ServerName = Application.StartupPath & "\DB.mdb"
ConInfo.ConnectionInfo.DatabaseName = "kuku.mdb"
ConInfo.ConnectionInfo.UserID = "Admin"
ConInfo.ConnectionInfo.Password = ""
Dim rpt1 As New CrystalReport5
rpt1.Database.Tables(0).ApplyLogOnInfo(ConInfo)
rpt1.SetDataSource(DataSet_PaymentsP)
rpt1.SetParameterValue("Start_Date", dtDate1)
rpt1.SetParameterValue("End_Date", dtDate2)
rpt1.SetParameterValue("CustomerName", txtCustomer.Text)
rpt1.SetParameterValue("Author", FormMain.XN.Text)
Dim frm As New FormPrint
frm.CrystalReportViewer1.ReportSource = rpt1
frm.CrystalReportViewer1.LogOnInfo(0).ConnectionInfo.Password = "yazsys.com1234"
frm.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I just did add the single quotation marks as shown below and the select query can now retrieve records fro ms access
SQL_PaymentsP = "SELECT Payments.PaymentID, Payments.InvoiceID,
Payments.CustomerName, Payments.InvoiceDate, Payments.InvoiceAmount,
Payments.PaymentDesc, Payments.PaidAmount, Payments.DatePaid, Payments.CurrentBalance, Payments.Status FROM Payments WHERE Payments.DatePaid Between '#" & dtDate1.ToString("MM/dd/yyyy") & "#' And '#" & dtDate2.ToString("MM/dd/yyyy") & "#' OR " & "Payments.CustomerName ='" & txtCustomer.Text & "'"
I'm attempting to input a variable declared as a string into my access database. When the button is clicked "Ticket Status" is not entered into the access database.
Dim str As String
str = "update [tblUsers] set [Campus] = '" & cmboxCampus.SelectedItem & "' , [ProblemType] = '" & cmboxProblemType.SelectedItem & "', [IfOther] = '" & txtIfOther.Text & "', [Status] = '" & TicketStatus & "', [ProblemDescription] = '" & txtDescription.Text & "' Where [Username] = '" & txtUsername.Text & "'"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
TicketStatus = "Ticket being Assigned"
If txtIfOther.ReadOnly = True And txtIfOther.Text <> "" Then
MsgBox("When selecting 'other' please specify")
End If
If txtDescription.Text = "" Or cmboxCampus.SelectedItem = "" Or cmboxProblemType.SelectedItem = "" Then
MsgBox("Please fill out all fields.", MsgBoxStyle.Information)
Else
Try
cmd.ExecuteNonQuery()
cmd.dispose()
myConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
MsgBox("Your ticket has been successfully submitted.", MsgBoxStyle.Information)
End If
End Sub
first, I would like to say that thank all of the programmers, people that make this website so efficient! I'm proud to say that 80% of my programming knowledge on VB I gained was because of all of the samples and answers in this webpage. Anyway, so I'm developing a Quality Control application for my company and there's a datagridview in one of my forms. The user can make changes to it and after that he/she has to save the datagrid back to the MS Access database. I tried everything and I can't save the date field into the database. I checked for field formatting and the database table is formatted to "Date/time"
here is what i have:
Dim sql As String
Try
For i As Integer = 0 To dataAddemdumView.RowCount - 1
sql = "UPDATE MasterRecordsT SET Fecha = '" & dataAddemdumView.Rows(i).Cells("Fecha").Value & "', Pass = " & dataAddemdumView.Rows(i).Cells("Pass").Value & ", Fail =
" & dataAddemdumView.Rows(i).Cells("Fail").Value & ", Employee = " & dataAddemdumView.Rows(i).Cells("Employee").Value & ", Gig = " & dataAddemdumView.Rows(i).Cells("Gig").Value & ", GigNotes =
'" & dataAddemdumView.Rows(i).Cells("GigNotes").Value & "', Department = '" & dataAddemdumView.Rows(i).Cells("Department").Value & "' WHERE ID = " & dataAddemdumView.Rows(i).Cells("ID").Value & ""
cmd = New OleDbCommand(sql, con)
con.Open()
da.UpdateCommand = con.CreateCommand()
da.UpdateCommand.CommandText = sql
da.UpdateCommand.ExecuteNonQuery()
con.Close()
Next
Catch ex As Exception
con.Close()
'MessageBox.Show("OPEX Quality encountered a problem, Try to reopen the application to solve issues", "Error 0002", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
thank you so much for your help guys!
so this would be the final code and it works
Dim sql As String
Try
For i As Integer = 0 To dataAddemdumView.RowCount - 1
If Not dataAddemdumView.Rows(i).Cells("Fecha").Value Is DBNull.Value Then
sql = "UPDATE MasterRecordsT SET Fecha = '" & dataAddemdumView.Item("Fecha", i).Value & "', Pass = " & dataAddemdumView.Rows(i).Cells("Pass").Value & ", Fail =
" & dataAddemdumView.Rows(i).Cells("Fail").Value & ", Employee = " & dataAddemdumView.Rows(i).Cells("Employee").Value & ", Gig = " & dataAddemdumView.Rows(i).Cells("Gig").Value & ", GigNotes =
'" & dataAddemdumView.Rows(i).Cells("GigNotes").Value & "', Department = '" & dataAddemdumView.Rows(i).Cells("Department").Value & "' WHERE ID = " & dataAddemdumView.Rows(i).Cells("ID").Value & ""
cmd = New OleDbCommand(sql, con)
con.Open()
da.UpdateCommand = con.CreateCommand()
da.UpdateCommand.CommandText = sql
da.UpdateCommand.ExecuteNonQuery()
con.Close()
End If
Next
Catch ex As Exception
con.Close()
MsgBox(ex.Message)
'MessageBox.Show("OPEX Quality encountered a problem, Try to reopen the application to solve issues", "Error 0002", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try