Data between years using select query in MS Access - vb.net

I want data between years using select query in MS Access in VB.NET.
My code is:
Try
Cursor = Cursors.WaitCursor
Dim rpt As New rptLedger()
Dim myConnection As OleDbConnection
Dim MyCommand As New OleDbCommand()
Dim myDA As New OleDbDataAdapter()
Dim myDS As New DataSet 'The DataSet you created.
myConnection = New OleDbConnection(cnString)
MyCommand.Connection = myConnection
MyCommand.CommandText = "SELECT Ledger.rdate, Ledger.pDate, Ledger.receipttotal, Ledger.PaymentTotal, Ledger.PartyName, Ledger.CompCode, Ledger.Cmpcode, Ledger.rnarration, Ledger.pnarration FROM Ledger where pDate >= dateadd(year,-1,GETD())"
MsgBox(MyCommand.CommandText)
MyCommand.CommandType = CommandType.Text
myDA.SelectCommand = MyCommand
myDA.Fill(myDS, "Ledger")
CrystalReportViewer1.ReportSource = rpt
myConnection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

You need Access functions and syntax:
MyCommand.CommandText = "SELECT Ledger.rdate, Ledger.pDate, Ledger.receipttotal, Ledger.PaymentTotal, Ledger.PartyName, Ledger.CompCode, Ledger.Cmpcode, Ledger.rnarration, Ledger.pnarration FROM Ledger where pDate >= DateAdd('yyyy', -1, Date())"

Related

When I insert a field into crystal report and that's the code the field does not appear in the report

This is the code on the print command
and the dataset connected with database successfully
Try
Dim rpt As New rptallergy2
Dim myConnection As SqlConnection
Dim MyCommand1 As New SqlCommand()
Dim myDA1 As New SqlDataAdapter()
Dim myDS As New DataSet
myConnection = New SqlConnection(cs)
MyCommand1.Connection = myConnection
MyCommand1.CommandText = "SELECT RTRIM(patient.name) from patient "
MyCommand1.CommandType = CommandType.Text
myDA1.SelectCommand = MyCommand1
myDA1.Fill(myDS, "patient")
rpt.SetDataSource(myDS)
frmReport.CrystalReportViewer1.ReportSource = rpt
frmReport.ShowDialog()
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
MyCommand1.CommandText = "SELECT RTRIM(patient.name) as pName from patient"
Then match the pName field to the field on your crystal reports is what Jonathan Willcock was saying.

Trouble loading data into textbox

Upon the click of a button, I want the program to load in details about the user (stored in an access database) into the textboxes.
This is what I have to far
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Administratot\Documents\RailwayDatabase2.accdb"
Dim MyConn As New OleDbConnection(connString)
Dim da As OleDbDataAdapter
Dim ds As DataSet
Try
MyConn.Open()
da = New OleDbDataAdapter("Select * from tbl_employees WHERE FirstName = """ & EmployeeLogin.usersname & """", MyConn)
ds = New DataSet
da.Fill(ds)
TextBox1.Text = ds.Tables("tbl_employees").Rows(0).Item(2)
MyConn.Close()
Catch ex As Exception
If MyConn.State <> ConnectionState.Closed Then MyConn.Close()
End Try
End Sub
any idea why this isn't working? thanks

between two date to show report crystal vb

Dim rpt As New CheckoutReportpreview
Dim cn As SqlConnection
Dim cmd As New SqlCommand
Dim myDA As New SqlDataAdapter
Dim ds As New CheckoutSet
Dim crConnectionInfo As New ConnectionInfo
Try
With crConnectionInfo
.ServerName = "."
.DatabaseName = "Hotel Management System"
.UserID = "sa"
.Password = "s123"
End With
cmd.Connection = cn
cn.Open()
Dim da As New SqlDataAdapter("select * from tblCheckout where CheckoutDate>='" & CDate(Date_from.Value) & "' and CheckoutDate<='" & CDate(Date_to.Value) & "' ", cn)
cmd.Prepare()
cmd.CommandType = CommandType.Text
myDA.SelectCommand = cmd
myDA.Fill(ds, "tblCheckout")
rpt.SetDataSource(ds)
CheckoutReport.Show()
CheckoutReport.CrystalReportViewer1.ReportSource = rpt
cn.Close()
Catch Excep As Exception
MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I HAVE A CODE HERE AND I WANT TO DISPLAY THE REPORT BETWEEN TO DATES TO OTHER FORM TO CLICK PREVIEW BUTTON, SO CAN YOU HELP ME WHEN I RUN THE MESSAGE APPEAR IS
object reference not set to an instance of an object
To answer your question on why you're getting a "object reference not set to an instance of an object" look at your SqlConnection. You declare it but never instantiate it so it's null/nothing when you reference it giving you that error. Your probably getting it on the cn.Open() line.
Change:
Dim cn As SqlConnection
to something like:
Dim cn As New SqlConnection("server=yourserver;database=yourdatabase;uid=sa=pwd=sa123")

Condition or Rule when Importing Excel data to SQL

At line 4 of my code, how or is it possible to have a condition in there?
For example:
Dim expr As String = "SELECT * FROM [Sheet1$] WHERE excelColumn1 <> NULL"
I have tried it, but it gives an Error: No value given for one or more required parameters.
Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\z.fontanilla\Documents\etl2.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=Yes""")
ExcelConnection.Open()
Dim expr As String = "SELECT * FROM [Sheet1$]"
Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
Dim objDR As OleDbDataReader
Dim SQLconn As New SqlConnection()
Dim ConnString As String = "Data Source=cyayay\sqlexpress;Initial Catalog=reportingDB;Integrated Security=True"
SQLconn.ConnectionString = ConnString
SQLconn.Open()
Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
bulkCopy.DestinationTableName = "tFalse"
Try
objDR = objCmdSelect.ExecuteReader
bulkCopy.BatchSize = 5000
bulkCopy.WriteToServer(objDR)
objDR.Close()
SQLconn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Using
ExcelConnection.Close()

How to call a query from MS Access 2010 (.accdb) within VB.Net 2010?

I have this code that populates a datagridview with data from ms access:
Dim con As New OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim Sql As String
Sql = "SELECT * FROM myTable WHERE case_no=?"
Try
con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.accdb;Persist Security Info=True;Jet OLEDB:Database Password=dbadmin2010"
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand(Sql, con)
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Case Info")
DataGridView1.DataSource = ds.Tables("Case Info")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Now I had just finished creating a query from design view within MS Access itself, is there a way to call that query and retrieve the results to my datagridview?
Just use the query name and set the command type, for example, in addition to what you already have, you can use the following notes:
Try
con.ConnectionString = enter_connection_string_here
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "NameOfQuery"
da.SelectCommand = cmd
cmd.Parameters.AddWithValue("case_no", case_no)
da.Fill(ds, "Case Info")