0

I'm struggling to grasp VB as I'm new to programming. I am creating a program which displays a result (namely a link to a website) based on a users' filters. I've done this so far, which just selects the size filter. However, I don't know how to write the code that will check the database for the users' preference and output an appropriate link in the form of a label. Is it as simple as just printing DataTable or am I totally wrong? Any help would be greatly appreciated.

Here is my code so far:

Imports System.Data.OleDb

Public Class frmFilters
    Dim provider As String
    Dim dataFile As String
    Dim ConnString As String
    Dim myConnection As OleDbConnection = New OleDbConnection
    Private ConStr As String = "My Connection String"

    Private Sub buttonSearch_Click(sender As Object, e As EventArgs) Handles buttonSearch.Click
        provider = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source="
        dataFile = "H:\Visual studio files\Project\CMPC\partLists.accdb"
    End Sub

    Private Function GetPartData(filterMicroATX As Integer, filterATX As Integer) As DataTable
        Dim dt As New DataTable
        Dim sql = "SELECT * FROM partList WHERE
                    [size] > @MicroATX 
                And [size] < @ATX;"
        Using con As New OleDbConnection(ConStr),
                cmd As New OleDbCommand(sql, con)
            With cmd.Parameters
                .Add("@MicroATX", OleDbType.Integer).Value = filterMicroATX
                .Add("@ATX", OleDbType.Integer).Value = filterATX

            End With
            con.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
        Return dt
    End Function

    Private Sub btnQuit_Click(sender As Object, e As EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub

End Class
GSerg
  • 76,472
  • 17
  • 159
  • 346
  • What you want to print from the _partList_ table? You have selected all fields and all records from that table, now all their values are in the _dt_ DataTable distributed in a rectangular shape made of Rows and Columns. You just need to index the Row and Column property to get the data you want. – Steve Jan 05 '21 at 18:23
  • In my table there is the link to the parts, price, size and usability, I just want to print the parts – Luke Cassidy Jan 05 '21 at 18:24
  • Unrelated to your question, but you will ultimately want to see https://stackoverflow.com/a/21925885/11683. – GSerg Jan 05 '21 at 18:27
  • @GSerg Maybe you had to do that in 2014 but not today. You can put `@Whatever` directly in you sql statements just as long as you add the parameter to the `ParametersCollection` in the same order that they appear in the sql statement. – Mary Jan 05 '21 at 21:58
  • Do you need all the fields in the table or just the field with the link? What is the name of the field in the database that contains the link? – Mary Jan 05 '21 at 22:02
  • I want to print the list of parts from the table. How would I index the row and column property correctly? – Luke Cassidy Jan 05 '21 at 23:52
  • I just need the field with the link. The name of the field in the database is called pList. – Luke Cassidy Jan 05 '21 at 23:53
  • @Mary I don't get your complaint. "As long as you add the parameter to the ParametersCollection in the same order" is exactly the problem. Replacing `@whatever` with `?` was never required, but it is a good thing to do to emphasis the fact that the names do not in fact do anything. – GSerg Jan 06 '21 at 08:01
  • @Steve apologies I forgot to use the @ so you may have not seen my comment. How would I go about indexing the row and column property? – Luke Cassidy Jan 07 '21 at 12:29
  • Suppose the result of your query is a table with two rows (records) and each row has two columns (record's fields). To get the second field of the second row you write _Dim value = dt.Rows(1)(1)_ Rows and columns are arrays and with arrays indexing start at index 0 – Steve Jan 07 '21 at 13:11

0 Answers0