Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / January 2006

Tip: Looking for answers? Try searching our database.

calling a function from a sub

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Wickham - 10 Jan 2006 03:14 GMT
Hi,
I have a Private sub activated by the OnClick event of a forms command
button.
The code within this sub adds records to a table.

I have borrowed some Function code from Allen Browne MVP that deletes
all records from a table and resets the AutoNumber field to "1".

They work perfectly as separate entities.

I now wish to combine the 2 by calling the Function that deletes the
records at the beginning of the sub that will add the records.

Can someone please explain the correct way to, firstly,
declare and then call the function from the sub.

Thankyou,
Bob
Rick Brandt - 10 Jan 2006 03:20 GMT
> Hi,
> I have a Private sub activated by the OnClick event of a forms command
[quoted text clipped - 14 lines]
> Thankyou,
> Bob

Just use the name of the function and pass any required arguments.

Sub ExampleSub

   Function arg1, arg2

   some other code

End Sub

Signature

I don't check the Email account attached
to this message.     Send instead to...
RBrandt    at       Hunter      dot      com

Bob Wickham - 10 Jan 2006 03:39 GMT
>>Hi,
>>I have a Private sub activated by the OnClick event of a forms command
[quoted text clipped - 24 lines]
>
> End Sub

I tried that already which is the way I expected it to work.

The function is
 Public Function DeleteAllAndResetAutoNum(strTable As String) As Boolean
    'Return:    True if sucessful.
    Dim cat As New ADOX.Catalog
    Dim tbl As ADOX.Table
    Dim col As ADOX.Column
    Dim strSql As String

    'Delete all records.
    strSql = "DELETE FROM [" & strTable & "];"
    CurrentProject.Connection.Execute strSql

    'Find and reset the AutoNum field.
    cat.ActiveConnection = CurrentProject.Connection
    Set tbl = cat.Tables(strTable)
    For Each col In tbl.Columns
        If col.Properties("Autoincrement") Then
            col.Properties("Seed") = 1
            DeleteAllAndResetAutoNum = True
        End If
    Next
End Function

I wish to call it from a sub.

which is

Private Sub Command2_Click()

  Dim dbs As Database
  Dim rstLoans As DAO.Recordset
  Dim rstMissed As DAO.Recordset
  Dim strSql As String
  Set dbs = CurrentDb

  Set rstLoans = dbs.OpenRecordset("SELECT DISTINCT LoanNo FROM
tblCommission")
  Do Until rstLoans.EOF
    Set rstMissed = dbs.OpenRecordset("SELECT [MonthYear]" & _
                   " FROM tblDate" & _
                   " WHERE [MonthYear] < Now()" & _
                   " AND Format([MonthYear],'mmmm,yyyy')" & _
                   " Not In (SELECT Format ([PaymentDate],'mmmm,yyyy')" & _
                              " FROM tblCommission" & _
                              " WHERE LoanNo =" & rstLoans!LoanNo & ")")

     Do Until rstMissed.EOF
          strSql = "INSERT INTO tblMissingCommissionReport (
LoanNumber, TheDate )" & _
          " VALUES ( " & rstLoans!LoanNo & ", " &
CLng(rstMissed!MonthYear) & " )"
          dbs.Execute strSql, dbFailOnError
          rstMissed.MoveNext
     Loop
     rstMissed.Close
     rstLoans.MoveNext
  Loop
  rstLoans.Close
  Set rstMissed = Nothing
  Set rstLoans = Nothing
  Set dbs = Nothing
End Sub

Do they both have to be in a Module or a Class Module or should it work
if they are both in the Form_Form1 class object.

Bob
Rick Brandt - 10 Jan 2006 03:47 GMT
> I tried that already which is the way I expected it to work.
> [snip code]
> Do they both have to be in a Module or a Class Module or should it
> work if they are both in the Form_Form1 class object.

Since the function is declared "public" then it could be in a standard module or
it could be in the same form module as the sub.

Once properly declared, calling a custom function is no different than calling
Date() or any other built in function.

Signature

I don't check the Email account attached
to this message.     Send instead to...
RBrandt    at       Hunter      dot      com

Bob Wickham - 10 Jan 2006 04:15 GMT
> Bob Wickham wrote:
>
[quoted text clipped - 3 lines]
> Once properly declared, calling a custom function is no different than calling
> Date() or any other built in function.

Thanks Rick,

You say "Once properly declared".

How is that done?

In C++, for instance, I would declare it at the beginning of the code by
stating the function name preceded by its return type.
eg. void myFunction();

and then where I wish to call the function I would type
void myFunction()

I'm trying something similar in vba but its not working.

Any clues?

Bob
Rick Brandt - 10 Jan 2006 12:28 GMT
> Thanks Rick,
>
> You say "Once properly declared".
>
> How is that done?

I just meant that it was properly constructed and scoped.  If you can call the
function separately then you are already there.

Please explain "not working".   Do you get an error?

Signature

I don't check the Email account attached
to this message.     Send instead to...
RBrandt    at       Hunter      dot      com

Bob Wickham - 10 Jan 2006 20:19 GMT
>>Thanks Rick,
>>
[quoted text clipped - 6 lines]
>
> Please explain "not working".   Do you get an error?

Hi Rick

The name of my function is
Public Function DeleteAllAndResetAutoNum(strTable As String) As Boolean

If I call it from my sub like this

Private Sub Command2_Click()

  Dim dbs As Database
  Dim rstLoans As DAO.Recordset
  Dim rstMissed As DAO.Recordset
  Dim strSql As String

  DeleteAllAndResetAutoNum

  Set dbs = CurrentDb

  Set rstLoans = dbs.OpenRecordset("SELECT DISTINCT LoanNo FROM
tblCommission")

some more code
some more code

I get a Compile Error - Argument not optional

If I call it like this

Private Sub Command2_Click()

  Dim dbs As Database
  Dim rstLoans As DAO.Recordset
  Dim rstMissed As DAO.Recordset
  Dim strSql As String

  Call DeleteAllAndResetAutoNum(strTable As String)

  Set dbs = CurrentDb

  Set rstLoans = dbs.OpenRecordset("SELECT DISTINCT LoanNo FROM
tblCommission")

some more code
some more code

I get a Compile Error - Syntax Error

Do I need to announce its existence along with the other Dim statements
(in other words "declare")

Some sample code of how it SHOULD be done would be the most helpful to me.

Thanks,
Bob
RoyVidar - 10 Jan 2006 20:42 GMT
Bob Wickham wrote in message <43c416e3@dnews.tpgi.com.au> :
> Hi Rick
>
[quoted text clipped - 50 lines]
> Thanks,
> Bob

Hi,

you are calling a function which returns a boolean. The function
expects one parameter. Try calling like this

Call DeleteAllAndResetAutoNum("NameOfYourTable")

to not "use or return" the return value or

dim fMyBoolean as boolean
fMyBoolean = DeleteAllAndResetAutoNum("NameOfYourTable")

then you can for instance test the boolean before allowing the rest
of the code to run.

Signature

Roy-Vidar

Bob Wickham - 10 Jan 2006 20:49 GMT
> Bob Wickham wrote in message <43c416e3@dnews.tpgi.com.au> :
>
[quoted text clipped - 68 lines]
> then you can for instance test the boolean before allowing the rest
> of the code to run.

Thanks Roy and Rick,

I just figured it out.
Its amazing how explaining it to someone else can make one see what is
really, really obvious.

Thanks again,
Bob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.