Results 1 to 11 of 11

Thread: [RESOLVED] Argument Not Optional (DB Connection)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Argument Not Optional (DB Connection)

    I am trying to connect to mysql database a different way than usual and getting an argument not optional error on the highlighted.

    FORM
    VB Code:
    1. Private Sub cmdAdd_Click()
    2.     Dim LvwItm As ListItem
    3.    
    4.     With Me
    5.         Set LvwItm = frmLedger.lvwAccountLedger.ListItems.Add(, , Format(Date, "mm/dd/yyyy"))
    6.         LvwItm.SubItems(1) = .txtTransactionDescription.Text
    7.         LvwItm.SubItems(2) = Format(.txtCredit.Text, "0.00")
    8.         LvwItm.SubItems(3) = Format(.txtDebit.Text, "0.00")
    9.     End With
    10.    
    11.     [hl]DatabaseConnection[/hl]
    12.    
    13.     strSQL = "SELECT * "
    14.     strSQL = strSQL & "FROM Ledger"
    15.     objCon.Execute strSQL
    16.    
    17.     OpenRecordset
    18.    
    19.     With frmLedger
    20.         strSQL = "INSERT INTO Ledger ("
    21.         strSQL = strSQL & "AccountNumber, "
    22.         strSQL = strSQL & "TransDesc, "
    23.         strSQL = strSQL & "Credit, "
    24.         strSQL = strSQL & "Debit, "
    25.         strSQL = strSQL & "EntryDate) "
    26.         strSQL = strSQL & "VALUES ("
    27.         strSQL = strSQL & "'" & .cboAccountNumber.Text & "', "
    28.         strSQL = strSQL & "'" & .txtTransactionDescription.Text & "', "
    29.         strSQL = strSQL & "'" & Format(.txtCredit.Text, "0.00") & "', "
    30.         strSQL = strSQL & "'" & Format(.txtDebit.Text, "0.00") & "', "
    31.         strSQL = strSQL & "'" & Now() & "')"
    32.     End With
    33.    
    34.     objCon.Execute strSQL
    35.    
    36.     Unload Me
    37. End Sub

    MODULE
    VB Code:
    1. Option Explicit
    2.  
    3. Public Sub DatabaseConnection(objCon As ADODB.Connection)
    4.     Set objCon = New ADODB.Connection
    5.    
    6.     With objCon
    7.         .CursorLocation = adUseClient
    8.         .ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
    9.                           & "SERVER=IP_ADDRESS;" _
    10.                           & "DATABASE=DATABASE_NAME;" _
    11.                           & "UID=USERNAME;" _
    12.                           & "PWD=PASSWORD;" _
    13.                           & "OPTIONS=3;"
    14.     End With
    15. End Sub
    16.  
    17. Public Sub OpenRecordset(objCon As ADODB.Connection, _
    18.                          objRs As ADODB.Recordset, _
    19.                          strSQL As String)
    20.    
    21.     Set objCon = New ADODB.Connection
    22.     Set objRs = New ADODB.Recordset
    23.    
    24.     objCon.Open strSQL, objCon, adOpenStatic, adLockReadOnly
    25. End Sub
    26.                          
    27. Public Sub CloseRecordset(objRs As ADODB.Recordset)
    28.     objRs.Close
    29.     Set objRs = Nothing
    30. End Sub
    31.  
    32. Public Sub CloseConnection(objCon As ADODB.Connection)
    33.     objCon.Close
    34.     Set objCon = Nothing
    35. End Sub

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Argument Not Optional (DB Connection)

    your sub call:
    VB Code:
    1. DatabaseConnection
    your sub:
    VB Code:
    1. Public Sub DatabaseConnection[B](objCon As ADODB.Connection)[/B]
    objCon is not an optional parameter

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Argument Not Optional (DB Connection)

    Error is quite obvious according to procedure declaration:

    DatabaseConnection(objCon As ADODB.Connection)

    Few different ways to overcome:

    - get rid of objCon As ADODB.Connection from within procedure header
    - use public object variable declaration
    - change procedure to function that return ado connection
    - or use code below:
    VB Code:
    1. Dim objCon As ADODB.Connection
    2.  
    3. DatabaseConnection [B]objCon[/B]
    4.  
    5. '... continue with your processing...

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Argument Not Optional (DB Connection)

    Quote Originally Posted by bushmobile
    your sub call:
    VB Code:
    1. DatabaseConnection
    your sub:
    VB Code:
    1. Public Sub DatabaseConnection[B](objCon As ADODB.Connection)[/B]
    objCon is not an optional parameter
    i didnt know that, thanks bush... learned something new

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Argument Not Optional (DB Connection)

    Quote Originally Posted by RhinoBull
    Error is quite obvious according to procedure declaration:

    DatabaseConnection(objCon As ADODB.Connection)

    Few different ways to overcome:

    - get rid of objCon As ADODB.Connection from within procedure header
    - use public object variable declaration
    - change procedure to function that return ado connection
    - or use code below:
    VB Code:
    1. Dim objCon As ADODB.Connection
    2.  
    3. DatabaseConnection [B]objCon[/B]
    4.  
    5. '... continue with your processing...
    so anything to do with ado can never be in a header declaration?

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Argument Not Optional (DB Connection)

    so anything to do with ado can never be in a header declaration?
    Sure it can. RB was giving you a few options.


    In the example you posted (as BM pointed out) you called a Sub that had an parameter - being in this case a 'Connection'.

    Looking at the rest of your code, the Connection is used in all your Subs too.

    So in this case, pass the Connection in the DatabaseConnection call as Rhino and bushmobile pointed out.


    EDIT: Whats confussing, is that your passing the Connection, but then Setting it to a New Connection in that Sub????????
    Last edited by Bruce Fox; Nov 21st, 2006 at 12:44 AM.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Argument Not Optional (DB Connection)

    Following this along, would this work:
    VB Code:
    1. 'Form Level
    2. [b]Dim objCon As ADODB.Connection[/b]   'This can then be [i]Set to Nothing[/i] when your done
    3.  
    4. Public Sub DatabaseConnection()  'Note, No parameter
    5.     Set objCon = New ADODB.Connection
    6.    
    7.     With objCon
    8.         .CursorLocation = adUseClient
    9.         .ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
    10.                           & "SERVER=IP_ADDRESS;" _
    11.                           & "DATABASE=DATABASE_NAME;" _
    12.                           & "UID=USERNAME;" _
    13.                           & "PWD=PASSWORD;" _
    14.                           & "OPTIONS=3;"
    15.     End With
    16. End Sub

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Argument Not Optional (DB Connection)

    so if i use a sub, then to use a public declaration... if i use a function then i can do
    VB Code:
    1. Public Function SomeSubName(objCon As ADODB.Connection)
    2. '....
    3. End Function
    ?

    Quote Originally Posted by Bruce Fox
    Sure it can. RB was giving you a few options.


    In the example you posted (as BM pointed out) you called a Sub that had an parameter - being in this case a 'Connection'.

    Looking at the rest of your code, the Connection is used in all your Subs too.

    So in this case, pass the Connection in the DatabaseConnection call as Rhino and bushmobile pointed out.


    EDIT: Whats confussing, is that your passing the Connection, but then Setting it to a New Connection in that Sub????????

  9. #9

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: [RESOLVED] Argument Not Optional (DB Connection)

    but the thing is that i dont understand is if i used a function, you have to have a datatype for the function name, what datatype would that be?

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Argument Not Optional (DB Connection)

    I'll try to explain one more time:
    - function returns value and sub doesn't
    - if you want function to return something then it must return some type (ado connection, recordset, integer, string, etc)
    - to return ado connection you declare it as adodb.connection but also you do not have to pass any arguments at all...

    Here is a quick sample:
    VB Code:
    1. Public Function DatabaseConnection() As ADODB.Connection
    2. Dim objCon As ADODB.Connection
    3.  
    4.     Set objCon = New ADODB.Connection
    5.    
    6.     With objCon
    7.         .CursorLocation = adUseClient
    8.         .ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
    9.                           & "SERVER=IP_ADDRESS;" _
    10.                           & "DATABASE=DATABASE_NAME;" _
    11.                           & "UID=USERNAME;" _
    12.                           & "PWD=PASSWORD;" _
    13.                           & "OPTIONS=3;"
    14.         .Open
    15.     End With
    16.    
    17.     DatabaseConnection = objCon
    18.  
    19. End Function
    20.  
    21. 'usage:
    22. Dim objNewCon As ADODB.Connection
    23.  
    24. Set objNewCon = New ADODB.Connection
    25. Set objNewCon = DatabaseConnection
    26.  
    27. 'continue...

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: [RESOLVED] Argument Not Optional (DB Connection)

    If you want to have a sub then try sample below:
    VB Code:
    1. Public Sub DatabaseConnection(objCon As ADODB.Connection)
    2.  
    3.     With objCon
    4.         .CursorLocation = adUseClient
    5.         .ConnectionString = "DRIVER={MySQL ODBC 3.51 Driver};" _
    6.                           & "SERVER=IP_ADDRESS;" _
    7.                           & "DATABASE=DATABASE_NAME;" _
    8.                           & "UID=USERNAME;" _
    9.                           & "PWD=PASSWORD;" _
    10.                           & "OPTIONS=3;"
    11.         .Open
    12.     End With
    13.  
    14. End Sub
    15.  
    16. 'usage:
    17. Dim objCon As ADODB.Connection
    18.  
    19. Set objCon = New ADODB.Connection
    20. DatabaseConnection objCon
    21.  
    22. 'continue...

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