?
Solved

VB.net Create a query inside an Access database

Posted on 2013-05-26
2
Medium Priority
?
593 Views
Last Modified: 2013-06-01
Hi. I am using the following VB.net code to create a view inside a SQL database.
What similar code would I use to create a query inside an Access database?


    Sub Create_SQL_View(ByVal oViewName As String, ByVal oSQL As String)
        Dim myConnection As SqlConnection = New SqlConnection(Globals.ThisAddIn.oRIGHT.lblConnectionString.Text)
        Dim myCommand As SqlCommand

        myConnection.Open()

        Dim S As String = "CREATE VIEW " & oViewName & " AS " & " " & oSQL

        myCommand = New SqlCommand(S, myConnection)
        myCommand.ExecuteNonQuery()

        myConnection.Close()
    End Sub
0
Comment
Question by:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 9

Accepted Solution

by:
skipper68 earned 2000 total points
ID: 39197950
How about:
Dim query As OleDbCommand = New OleDbCommand(SELECT * FROM MYTABLE)  
 

or you can create a Query and store it into Access database file with VB.NET code  like this:

Imports System.Data.OleDb  
 
Public Class Form1  
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
 
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")  
 
        Dim cmd As OleDbCommand = New OleDbCommand("CREATE PROC procProductsList AS SELECT * FROM Table1", con)  
        con.Open()  
        cmd.ExecuteNonQuery()  
        con.Close()  
 
    End Sub  
 
End Class  

You can call/execute the Query stored in Access database file with VB.NET code like this:

 
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click  
        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")  
        Dim cmd As New OleDbCommand  
        cmd.CommandType = CommandType.StoredProcedure  
        cmd.CommandText = "procProductsList"  
        cmd.Connection = con  
        con.Open()  
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(cmd)  
        Dim ds As DataSet = New DataSet()  
        da.Fill(ds, "Table1")  
        DataGridView1.DataSource = ds.Tables("Table1")  
        con.Close()  
    End Sub
0
 

Author Closing Comment

by:Murray Brown
ID: 39212497
Thanks very much
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question