Solved

VB.net Create a query inside an Access database

Posted on 2013-05-26
2
569 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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