connecting to database from activeX control

  • 11 years ago

    Am I missing something, this works fine on a normal form. but on my control, it pops up a dialog box with the title "select data source" when I run it

    This is my first activeX control I've ever created

    dim db as Database

     Set db = OpenDatabase(txtDBPath.Text)

    PS: it's a MS Access database

  • 11 years ago

    Have you added a watch to txtDBPath.Text? Does it contain the right path? Last time I did this I had to use:
    dbpath = App.Path
    If Right$(db
    path, 1) <> "\" Then dbpath = dbpath & "\"
    txtDBPath.Text = db_path & "Filename.mdb"

    The only other thing I can think of is maybe the parameters are required:
    Set db = OpenDatabase(txtDBPath, False, False)





  • 11 years ago

    thanks sync_or_swim, the txtDBPath textbox doesn't get my path until it my form has finished loading.

    Is there a way to fix this. Here is my code

    On control:

    Public strPath As String
    Public strConnectSQL As String
    Private mstrFormula As String

     

    'Public Property Let Formula(ByVal NewValue As String)
     '  mstrFormula = NewValue
      ' PropertyChanged "Formula"
    'End Property


    'Public Property Get Formula() As String
    'Formula = frmbuilder.txtSQL
    'End Property

     


    Public Function Build(strPath As String, strConnectSQL) As String
        frmbuilder.txtDBPath = strPath
        frmbuilder.txtSQL.Text = strConnectSQL
        frmbuilder.Show vbModal
        If frmbuilder.okclicked = True Then
        frmbuilder.okclicked = False
        Build = frmbuilder.txtMyFormula
        Unload frmbuilder
        Else
        Build = "Cancelled"
        End If
       
    End Function












    Form in control Project (frmbuilder):

    Public okclicked As Boolean

    Private Sub CancelButton_Click()
    Unload Me
    End Sub


    Private Sub Form_Load()
    Dim db As Database
    'MsgBox txtDBPath.Text

     Set db = OpenDatabase(txtDBPath.Text)
     
     
     'I will hide these textboxes when control is working properly
     'txtDBPath.Visible = False
     'txtSQL.Visible = False



     txtMyFormula.Text = ""
     






    'Set rstrecordset = db.OpenRecordset("SELECT * FROM [C4INjust a test]")

     MsgBox txtDBPath.Text
     
     TreeView1.Nodes.Add , , "INPUTS", "Inputs"
     


    End Sub

     

    Private Sub OKButton_Click()
    okclicked = True
    Formula = frmbuilder.txtSQL


    Me.Hide

    End Sub

    standard project

    added control to my form, calling like this

    Private Sub Command2_Click()
    MsgBox Builder1.Build(App.Path & "\products.mdb", "SELECT * FROM tbltest")

    End Sub

  • 11 years ago

    Embarrassed [:$] fixed it by adding the code to  Sub Form_Activate()

    instead of  Private Sub Form_Load()

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”