# oledbcommand syntax (Please help!)



## ckjaime (Apr 18, 2007)

I am trying to teach myself visual basic and am writing a prorgram that connects to an access database and reads in a table. The command that I am using to select the table is listed below.

MyCommand = New OleDbCommand("Select * from Table1", MyConnection)

My questions is whether or not I can use a variable as the table name instead of 'Table1'. I want the table which is read in to change depending on the value of a variable. Thanks in advance.


----------



## shuuhen (Sep 4, 2004)

Well, the way things work in C# (similar to VB.NET, but it was introduced _for_ .NET) is you can concatenate strings by using the '+' operator (no quotes). I'm not sure if it works the same in VB.NET (I assume that's the version of VB you're using), but you can try it out (I don't do VB, since I know C# and prefer C over that when possible).

So you can call your method, then concatenate the strings where the method arguement belongs.

Similar to how you do things:

CommandVar = New ObjectInitMethod("Common text " + variable, OtherArguement);


----------



## Deckard (May 8, 2006)

VB uses & to concatenate, but shuuhen is right -- you can. You'd probably want something similar to:


```
Table = "Table1"
MyCommand = New OleDbCommand("Select * from " & Table, MyConnection)
```


----------



## ckjaime (Apr 18, 2007)

Thanks for the help! Both of these options worked. I ended up going with 


```
MyCommand = New OleDbCommand("Select * from " + Table, MyConnection)
```


----------

