dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Table name as variable

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    5

    Resolved [RESOLVED] Table name as variable

    Hi,

    I have made a small application at work and at some point, I need to search a specific Access table depending on user's choice.
    So I've made a dataset with the Visual Basic designer but of course I don't know which table of the dataset I will have to search as it dépends on user's choice, so my question is:

    If I use this code:

    Code:
    Dim s As String = "primaryKeyValue"
    Dim foundRow As DataRow = DataSet1.Tables("AnyTable").Rows.Find(s)
    
    If foundRow IsNot Nothing Then
        MsgBox(foundRow(1).ToString())
    Else
        MsgBox("A row with the primary key of " & s & " could not be found")
    End If
    Can I use a variable instead of "AnyTable"?
    If not, what can I do to search the right table?

    Thanks 😉

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,402

    Re: Table name as variable

    Sure you can. Give it a try.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,120

    Re: Table name as variable

    Why do you need to ask us whether it will work when you can just do it and see for yourself?

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    5

    Re: Table name as variable

    Quote Originally Posted by jmcilhinney View Post
    Why do you need to ask us whether it will work when you can just do it and see for yourself?
    Just because I don't have access to my computer for a few days so I can't try myself right now.
    I'm searching on the web for means to solve difficulties I have in the developpement of my app but I couldn't find anything on this... so I asked.
    Thanks for the answer ��
    Last edited by Dzib; Mar 25th, 2020 at 08:29 PM.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,704

    Re: Table name as variable

    Quote Originally Posted by Dzib View Post
    Just because I don't have access to my computer for a few days so I can't try myself right now.
    I'm searching on the web for means to solve difficulties I have in the developpement of my app but I couldn't find anything on this... so I asked.
    Thanks for the answer ��
    Interesting times. The mind keeps working while, at the moment, you may not have the tools. Yeah, a string is a string, whether it is in a variable or a string literal as you used in your example. They are essentially interchangeable.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    5

    Re: Table name as variable

    Quote Originally Posted by Shaggy Hiker View Post
    Interesting times. The mind keeps working while, at the moment, you may not have the tools.
    Unfortunately yes, even at night
    Thanks mate

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,120

    Re: Table name as variable

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, a string is a string, whether it is in a variable or a string literal as you used in your example. They are essentially interchangeable.
    Which is already apparent from the code posted:
    Quote Originally Posted by Dzib View Post
    Code:
    Dim s As String = "primaryKeyValue"
    Dim foundRow As DataRow = DataSet1.Tables("AnyTable").Rows.Find(s)

  8. #8
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    2,309

    Re: Table name as variable

    I would make a reusable Function, you just pass the Sql to that
    it will return True or False


    Code:
      Public Shared Function recordExist(ByVal sSQL As String, ByVal sTable As String) As Boolean
            Dim sDB As String = "E:\Adressen.mdb"
            Dim sCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                    "Data Source=" & sDB & ";"
    
            Dim totalRow As Long = 0
            'Set the Data Adapter
            Dim da As New OleDbDataAdapter(sSQL, sCon)
            Dim ds As New DataSet()
            da.Fill(ds, sTable)
    
            totalRow = Convert.ToInt32(ds.Tables(sTable).Rows.Count)
            If totalRow > 0 Then
                Return True
            Else
                Return False
            End If
        End Function
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width