Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to import the records from an Access database for the current month and need the records to be displayed in my datagridview on form load not a button click. My reason for this is if a end user opens the program it could take some time to load all the records, so I want to only retrieve the records entered for the current month and obviously year when the form loads. I am new to coding hence the reason I'm asking the people on here with loads of experience. I have no idea of who to do this.

VB
Public Sub GetData()
 
    con.Open()
 
    Dim dt As New DataTable("tbLMPH")
    Dim rs As New OleDb.OleDbDataAdapter("Select * from tbLMPH where IDday='" & TextBox1.Text & "' ", con)
    rs.Fill(dt)
 
    DataGridView1.DataSource = dt
    DataGridView1.Refresh()
 
    Label1.Text = dt.Rows.Count
 
    rs.Dispose()
 
    con.Close()
 
    If Val(Label1.Text) = 1 Then
 
        Dim i As Integer
        i = DataGridView1.CurrentRow.Index
 
        TextBox2.Text = DataGridView1.Item(1, i).Value
        TextBox3.Text = DataGridView1.Item(2, i).Value
        TextBox4.Text = DataGridView1.Item(3, i).Value
        TextBox5.Text = DataGridView1.Item(4, i).Value
        TextBox6.Text = DataGridView1.Item(5, i).Value
        TextBox7.Text = DataGridView1.Item(6, i).Value
 
    End If
    ' Display_Data()

End Sub
Posted
Updated 21-Jul-15 1:44am
v2
Comments
Maciej Los 21-Jul-15 16:48pm    
Does IDDay is numeric value or string?
Jaybo007 22-Jul-15 0:34am    
It is saved on the DB as Text(string). I did change it to number(Integer) but it didn't change anything.

1 solution

First of all instead of concatenating a value from textbox to your SQL query always use parameters, see OleDbParameter[^]

What comes to fetching the you can use Year and Month functions. For example if the IDday column is the one you want to compare then something like
VB
Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM tbLMPH WHERE Year(IDday) = Year(Now()) AND Month(IDday) = Month(Now())", con)
 
Share this answer
 
Comments
Jaybo007 21-Jul-15 8:29am    
Will this work if I have the month and year in 2 seperate columns? & do I need to format these colums to a date format? or can I leave these columns as numbers?
Wendelius 22-Jul-15 1:28am    
Do not store the date parts in separate columns. In the long run it will make your life much harder. Use date data type.
Jaybo007 21-Jul-15 8:30am    
If so I obviously need to change IDDay to the appropriate columns.
Jaybo007 21-Jul-15 8:40am    
Just wondering if this is correct and that I'm understanding what you asking me to change within the code.

Public Sub GetData()
con.Open()
Dim dt As New DataTable("tbLMPH")
Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM tbLMPH WHERE Year(IDday) = Year(Now()) AND Month(IDday) = Month(Now())", con)
rs.Fill(dt)
DataGridView1.DataSource = dt
DataGridView1.Refresh()
Label1.Text = dt.Rows.Count
rs.Dispose()
con.Close()
If Val(Label1.Text) = 1 Then
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TextBox2.Text = DataGridView1.Item(1, i).Value
TextBox3.Text = DataGridView1.Item(2, i).Value
TextBox4.Text = DataGridView1.Item(3, i).Value
TextBox5.Text = DataGridView1.Item(4, i).Value
TextBox6.Text = DataGridView1.Item(5, i).Value
TextBox7.Text = DataGridView1.Item(6, i).Value
End If
' Display_Data()
End Sub
Wendelius 22-Jul-15 1:30am    
If IDday is the column containing the date you want to use in comparison, looks good. Just a minor thing: If you want to fetch records starting from this month, use >= in the comparison instead of =

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900