Click here to Skip to main content
15,074,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello can someone assist me, how to make this correct.
I want to make function for select in ms access file

First i have Func.vb
VB
Public Sub ReadTables(ColumNamer As String, Datatable As String)
Try
            With cmd
                .Connection = conn
                .CommandText = "Select * From @ColumnName"
                .Parameters.AddWithValue("@ColumnName", ColumNamer)

                ds.Tables.Add(dt)
                Adapter.SelectCommand = cmd
                Dim cb = New OleDbCommandBuilder(Adapter)
                cb.QuotePrefix = "["
                cb.QuoteSuffix = "]"
                Adapter.Fill(dt)
                Datatable.DataSource = dt.DefaultView
                .Parameters.Clear()
            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            Adapter.Dispose()
            dt.Dispose()
            cmd.Dispose()
            If conn IsNot Nothing Then
                conn.Close()
            End If
        End Try
    End Sub


Then in my Form2 i have added Datagridview1
and in Form load i want to send the information/receive to load in Form2 Results

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ReadTables("Accounts",Datagridview1)


How to do that?
- Because i want to use this function for more call's in different Tables in ms access.
- First call to be "Table name" to call , Second is "DataGridView" name to call

In my form2 datagridview is called "datagridview1" so if i had renamed it in the
ReadTables("Accounts","Renamed Datagridview") and to display the results when form loads

What I have tried:

When i try use the code inside Form2, works correct, but when try use function nothing shows Datagridview1 in Form2 is not active at all no results shows.
No error messages shows.

Edit: i forgot to put Datagridview1 - From "Friend" to "Public" but still not work correct
Posted
Updated 6-Jun-20 2:26am
v2
Comments
Richard MacCutchan 6-Jun-20 7:57am
   
You are calling ReadTables with two parameters, a string and a DataGridView reference. But your function declaration requires two strings.
diablo22 6-Jun-20 8:10am
   
yes that why i write topic, for support how should be where is the problems tips to solve it.
Richard MacCutchan 6-Jun-20 8:21am
   
Are you saying that you do not understand how to write and call a function?
F-ES Sitecore 6-Jun-20 8:30am
   
Are you sure you don't mean something like

Public Sub ReadTables(ColumNamer As String, Datatable As DataGridView)
diablo22 6-Jun-20 8:40am
   
if i use like Datatable As DataGridView
it cannot do the function Datatable = dt.DefaultView
F-ES Sitecore 6-Jun-20 8:41am
   
Make it whatever type "Datagridview1" is.
diablo22 6-Jun-20 8:49am
   
if i make it (Datatable As DataGridView1) it will be underline this name as option
Richard MacCutchan 6-Jun-20 9:17am
   
You cannot do that DataGridView1 is an object not a type; use DataGridView, which is the actual type. However you still need to check that subroutine to refer to the correct variable names.

1 solution

VB
.CommandText = "Select * From @ColumnName"
.Parameters.AddWithValue("@ColumnName", ColumNamer)
You can't do that - it won't work. The table name is evaluated and checked before the parameter substitution phase of comment execution in SQL.

You could concatenate the string, but you'd nave to do some extra work to ensure that it's safe or you leave yourself open to SQL Injection.

To add to that, it won't even compile: Datatable is a string:
VB
Public Sub ReadTables(ColumNamer As String, Datatable As String)

And strings do not have a DataSource property:
VB
Datatable.DataSource = dt.DefaultView
   
Comments
diablo22 6-Jun-20 8:42am
   
thanks for information it is usefull to know that cannot call the column name in query.
but any ideas how to then call only datagridview name to display the results even if i remove the table name
OriginalGriff 6-Jun-20 9:05am
   
Read what I said ... your parameter is a string, not a DataTable.
diablo22 6-Jun-20 9:19am
   
yes i sow that and i changed it to -
DatatableName As DataTable
DatatableName = dt
but in form2 doesnt display columns when load

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