Vb. Dynamic create Access database in net

Source: Internet
Author: User
Tags exit integer modify access database
access| Create | dynamic | data | database Imports ADOX

Module Module1

Public Function Createaccessdb (ByVal newdbpathname as String) as Integer
Dim I as Integer = 1
Dim Cat as Catalog = New Catalog ()
Try
Cat. Create ("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data source=" & Newdbpathname & _
"Jet oledb:engine type=5")

' Console.WriteLine ("Database Created successfully")
Catch
i = 0
MsgBox ("Error number:" & Err.Number.ToString & "error description:" & Err.Description.ToString)

End Try


Cat = Nothing
return I
End Function

End Module

Imports System.IO
Imports ADOX

Public Class Form1
Inherits System.Windows.Forms.Form
Dim filename as String = ""

#Region "code generated by the Windows forms Designer"

Public Sub New ()
MyBase.New ()

' This call is required by the Windows Forms Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Form overrides disposition to clean up the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
Friend WithEvents Button1 as System.Windows.Forms.Button
Friend WithEvents SaveFileDialog1 as System.Windows.Forms.SaveFileDialog
Friend WithEvents SaveFileDialog2 as System.Windows.Forms.SaveFileDialog
Friend WithEvents Button2 as System.Windows.Forms.Button
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
Me.button1 = New System.Windows.Forms.Button ()
Me.savefiledialog1 = New System.Windows.Forms.SaveFileDialog ()
ME.SAVEFILEDIALOG2 = New System.Windows.Forms.SaveFileDialog ()
Me.button2 = New System.Windows.Forms.Button ()
Me.suspendlayout ()
'
' Button1
'
Me.Button1.Location = New System.Drawing.Point (160, 104)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size (232, 48)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Create an Access database"
'
' SaveFileDialog1
'
Me.SaveFileDialog1.FileName = "Doc1"
'
' SaveFileDialog2
'
Me.SaveFileDialog2.FileName = "Doc1"
'
' Button2
'
Me.Button2.Location = New System.Drawing.Point (160, 192)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size (224, 56)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Build Dataase"
'
' Form1
'
Me.autoscalebasesize = New System.Drawing.Size (6, 14)
Me.clientsize = New System.Drawing.Size (592, 286)
Me.Controls.AddRange (New System.Windows.Forms.Control () {me.button2, me.button1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.resumelayout (False)

End Sub

#End Region

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim str1 as String = "D:\mrfu\newaccess"
Dim str2 as String = "Newaccess"

If file.exists ("D:\mrfu\newmdb.mdb") Then
MsgBox ("The file have exists!")
Exit Sub

End If
If Createaccessdb (str1) Then
MsgBox ("sucess!")
Else
MsgBox ("failed!")

End If
' Me. SaveFileDialog1

End Sub

Private Sub button2_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button2.click

With Me.savefiledialog1
. AddExtension = True

. Defaultext = "MDB"
. Checkpathexists = True
. Filter = "(Access database) *.mdb|*.mdb"
. Overwriteprompt = True

. FilterIndex = 1
End With


If Me.SaveFileDialog1.ShowDialog () = DialogResult.OK Then


filename = Me.SaveFileDialog1.FileName
If filename <> "" Then
MsgBox (filename)
Else
Exit Sub

End If
End If

If file.exists (filename) Then
MsgBox ("The file have exists!")
File.delete (filename)
MsgBox ("had delete!")

End If

filename = filename & ";"
MsgBox (filename)

If createaccessdb (filename) Then
MsgBox ("sucess!")
Else
MsgBox ("failed!")

End If
End Sub
End Class





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.