Adding DateTime as a NEW column on my database - vba

I'm getting syntax error on the part of ALTER TABLE etc. please help thanks in advance :) Visual Basic
Dim cnn As New OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb")
Dim mydate As DateTime
mydate = Me.DateTimePicker1.Value
Dim AddCol = "ALTER TABLE Attendance " & _
"ADD '" & mydate & "');"
Using cmd = New OleDbCommand(AddCol, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
End Using

I come up with this code and it's now working. thanks y'all! :)
Dim cnn As New OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb")
Dim mydate As DateTime
mydate = DateTimePicker1.Value
mydate.ToString("yyyy-MM-dd")
Dim AddCol = "ALTER TABLE Attendance " & _
"ADD '" & mydate & "' varchar(100);"
Using cmd = New OleDbCommand(AddCol, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
End Using

You haven't closed a string
Dim cnn As New OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection
cnn.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Users\Ashe\Documents\Visual Studio 2010\Projects\WindowsApplication1\Guidance System.accdb")
Dim mydate As DateTime
mydate = Me.DateTimePicker1.Value
Dim AddCol = "ALTER TABLE Attendance " & _
"ADD '" & mydate & "' varchar(100);"
Using cmd = New OleDbCommand(AddCol, cnn)
cnn.Open()
cmd.ExecuteNonQuery()
End Using
Note: Added varchar(100) after the last comment was posted to have a working code here too.

Related

vb.net getting value from a new name sql database

Dim cd As SqlCommand = New SqlCommand("select datepart(mm,Birthday) as MonthDates from Information where Name='" & cbname.Text & "'", con)
Dim reader1 As SqlDataReader = cd.ExecuteReader
While reader1.Read
Dim mon As Integer = reader1("MonthDates")
lblbday.Text = mon
I used this code to assume that the new named column in SQL will be read in vb net but its not reading the new named Column "MonthDates".
What is wrong with the code? Please help. Thank you in advance!
You should dispose connection after query (in my example USING do this):
Using con As New SqlConnection(....)
Dim cd As SqlCommand = New SqlCommand("select datepart(mm,Birthday) as MonthDates from Information where Name='" & cbname.Text & "'", con)
Dim mon As Integer = cd.ExecuteScalar()
lblbday.Text = mon
End Using

Search between two dates in access database using sql

This is my code for search in access database 2010. My problem is that when I search between two datetimepicker the result is wrong in datagridview, I mean when I search from specific records between May and June it shows me records also from February.
Private Sub Search_Record()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
Dim bookdetials As New frmContactDetails
Try
'get connection string declared in the Module1.vb and assing it to conn variable
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT contact_id, first_name , birth_date, book_num, send_from, no_answer, no_answer_by, rec, book_answer_name, book_answer_num, send_date, send_to, project_name FROM tblAddressBook"
If CheckBox1.Checked = True Then
sSQL = sSQL & " where project_name like '%" & Me.TextBox2.Text & "%' " & _
" AND birth_date between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
Me.dtgResult.DataSource = dt
Label4.Text = dt.Rows.Count
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
datepicker text should be converted to datetime format in sql
I had the same problem, the solution was too silly but it worked
use text instead of datetime in the db
make sure the datetimepicker enters "short format" data

Getting "data type mismatch in criteria expression" when I am trying to save date in excel vb form

I have a user form from which I want to save data into excel, I can save a normal string and integer but when I am trying to save data from Label13(contains date selected from calendar) in below code, it gives me error as "data type mismatch in criteria expression"
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "D:\Hotel\RoomAvailability.xls"
Dim Sqlq As String
Dim Booked_From As string = Label13.Text
Dim Room_Number As String = 202
MsgBox(Booked_From)
MsgBox(Room_Number)
Sqlq = "Update [Sheet1$] Set [Booked_From] = "
Sqlq = Sqlq & "'" & Booked_From & "'"
Sqlq = Sqlq & " where [Room_Number] ="
Sqlq = Sqlq & "'" & Room_Number & "'"
MsgBox(Sqlq)
MyConnection = New System.Data.OleDb.OleDbConnection( "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter(Sqlq, MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
DataGridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
Also what should be the format of cell in Excel in which i want to store date from Label13 ?

Error on .ExecuteNonQuery() in SQL Update Query [duplicate]

This question already has an answer here:
ADD SQL QUERY STAT
(1 answer)
Closed 8 years ago.
I'm trying to update an Access database using a SQL query, whenever I click the save button, it generates an error
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: No value given for one or more required parameters.
And highlights .ExecuteNonQuery(). Can you guys help me on this? I'm new to vb.net.
Thanks in advance.
Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
Dim empNum As String
Dim empFname As String
Dim empLname As String
Dim empDept As String
Dim empStat As String
Dim empYears As String
empNum = eNumText.Text
empFname = empFnameText.Text
empLname = empLnameText.Text
empDept = DeptText.Text
empStat = StatText.Text
empYears = yearstext.Text
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= c:\Databse\Company_db.accdb"
con.Open()
MsgBox(empNum)
Dim SqlAdapter As New OleDbDataAdapter
Dim Table As New DataTable
Dim sqlQuery As String = "UPDATE tbl_empinfo SET EmpID='" & empNum & "', FirstName ='" & empFname & "', LastName='" & empLname & "', Department='" & empDept & "', Status='" & empStat & "', Years='" & empYears & "' WHERE EmpID ='" & empNum & "' "
Using cmd As New OleDbCommand(sqlQuery, con)
With cmd
.CommandText = sqlQuery
.Connection = con
.Parameters.AddWithValue("EmpID", empNum)
.Parameters.AddWithValue("FirstName", empFname)
.Parameters.AddWithValue("LastName", empLname)
.Parameters.AddWithValue("Department", empDept)
.Parameters.AddWithValue("Status", empStat)
.Parameters.AddWithValue("Years", empYears)
.ExecuteNonQuery()
End With
End Using
sqlQuery = "SELECT * FROM tbl_empinfo "
Dim cmd1 As New OleDbCommand
Dim da As New OleDbDataAdapter
With cmd1
.CommandText = sqlQuery
.Connection = con
With SqlAdapter
.SelectCommand = cmd1
.Fill(Table)
End With
With DataGridView1
.DataSource = Table
End With
End With
con.Close()
End Sub
your query syntax is wrong. Since you are using params, use placeholders in the SQL: (the question marks are not some 'etc' type thing, you use ? to mark parameters!):
Dim sqlQuery As String = "UPDATE tbl_empinfo SET FirstName = ?,
LastName=?, Department=?,
Status=?, Years=? WHERE empID = ?"
Note: Six parameters
' USING will dispose of the cmd when it is done with it
' ...can also set the SQL and connection props in the constructor:
Using cmd As New OleDbCommand(sqlQuery, con)
With cmd
' no reason to move Textboxes to a variable either:
.Parameters.AddWithValue("#p1", empFnameText.Text)
.Parameters.AddWithValue("#p2", empLnameText.Text)
.Parameters.AddWithValue("#p3", DeptText.Text)
.Parameters.AddWithValue("#p4", StatText.Text)
.Parameters.AddWithValue("#p5", yearstext.Text)
your missing 6th parameter:
.Parameters.AddWithValue("#p6", eNumText.Text)
.ExecuteNonQuery()
End With
End Using
I dont think Access supports named params, so you use dummy ones but be sure to AddWithValue in the order specified in the SQL string.
EDIT
You can just create a SQL string with the values embedded instead of using params which is sort of what your SQL string does. Params are much better (research SQL injection attacks), but your string method is wrong (and you cant mix methods). It should be:
Dim sqlQuery As String = "UPDATE tbl_empinfo " &
"SET FirstName = " & empFname & ", LastName=" & empLname
The variables have to be outside the quotes or you will be setting FirstName to the literal "empFname"

multiple parameter selection in crystal report in .net not working

Form contains 1 texbox to hold Student ID and two Date-time Picker to select fees paid month by student and i am able to select date range of all students , where unable to filter fromdate to Todate with particular one Student.
i am getting error ....Please Help....... here is my problem code
Dim dbCommand As New OleDb.OleDbCommand
Dim myreport As New CrystalReport2
Dim Dt As New DataTable
Dim da As New OleDb.OleDbDataAdapter
dbCommand.CommandText = "Select FeesMonth between #" & FromDate.Text & "# and #" & ToDate.Text & "# From StudentPayments Where StudentID= '" & TextBox1.Text & "'"
da.SelectCommand = dbCommand
da.Fill(Dt)
myreport.SetDataSource(Dt)
CrystalReportViewer1.ReportSource = myreport
CrystalReportViewer1.Show
you have to give Connectionstring to dbCommand.
like -
OleDbConnection con=new OleDbConnection ();
dbCommand.Connectionstring=con;