Results 1 to 13 of 13

Thread: [RESOLVED] What is wrong with this ADO statement

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Resolved [RESOLVED] What is wrong with this ADO statement

    I need to be flexible when I open the mdb at customer sites. The line that is commented out works fine but the one I am trying under it errors:

    Code:
    Option Explicit
    'Private Const CNXSTRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Aces Projects\GL\database\AcesGL.mdb;Persist Security Info=False"
    Private Const CNXSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    
    Private cnx As ADODB.Connection
    Private rs As ADODB.Recordset
    Private aRs As ADODB.Recordset
    Private Prs As ADODB.Recordset
    I am following a textbook; however, I get an error saying:
    "Constant Expression Required"

    App.Path is correct and has a value of "C:\Aces Projects\GL"

    Thanks in advance for help.

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: What is wrong with this ADO statement

    change
    Code:
    Private Const CNXSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    to
    Code:
    Private CNXSTRING  as string= "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    __________________
    Rate the posts that helped you

  3. #3
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: What is wrong with this ADO statement

    as you can not concatenate two string in declaration if variable is of type CONST
    __________________
    Rate the posts that helped you

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: What is wrong with this ADO statement

    Sorry, it still gives me an error. It now highlights the "=" and says "expected end of statement". I think I copied it correctly:

    Code:
    Private CNXSTRING as string= "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: What is wrong with this ADO statement

    not sure what went wrong but just give one more try with below

    Code:
    Private CNXSTRING as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    __________________
    Rate the posts that helped you

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: What is wrong with this ADO statement

    Same result

  7. #7
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: What is wrong with this ADO statement

    what language you are using?is it vb.net?
    __________________
    Rate the posts that helped you

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: What is wrong with this ADO statement

    No, it is vb6

  9. #9
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: What is wrong with this ADO statement

    ok,not very much familiar with vb6 syntax but one of below should work..

    Code:
    dim CNXSTRING as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    or

    Code:
    dim CNXSTRING  = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    __________________
    Rate the posts that helped you

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: What is wrong with this ADO statement

    I got it working with some help. I had to declare the variable first. I then loaded the concatenated value to it separately and it now works! Thanks for responding.

  11. #11
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [RESOLVED] What is wrong with this ADO statement

    Glad to know your problem is solved,it would be good if you can post your solution so it can be helpfull to someone else in future
    __________________
    Rate the posts that helped you

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    325

    Re: [RESOLVED] What is wrong with this ADO statement

    Well, this does work if you "hardwire" the database location:

    Code:
    'Private Const CNXSTRING As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\GL.mdb;Persist Security Info=False"
    This does not work but I am not sure why. It has something to do with value restrictions I was told.

    [CODE]
    Private CNXSTRING as string = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
    [CODE]

    So, to make it work, in the top of the form, I put:

    Code:
    Private CNXSTRING As String
    And in the form_Initialize routine I put this"
    Code:
        Set cnx = New ADODB.Connection
        CNXSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\database\AcesGL.mdb;Persist Security Info=False"
        cnx.Open CNXSTRING
    Hope this helps someone.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED] What is wrong with this ADO statement

    That behaviour is expected..

    App.Path is not a fixed value (it can change each time you run the program), so is not a constant value - and therefore the compiler cannot hard-code it.

    In Classic VB you cannot declare and set a variable in one line of code, you need to use separate declaration and assignment statements (but Consts are the opposite).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width