UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
> Executing A Module On Form Load?    
 
   
JV63
post Jan 13 2017, 09:32 AM
Post#1



Posts: 197
Joined: 16-August 12



Hi. I have a module I want to have run when my form loads. I thought just doing a call would work but I get a popup saying "reserved error" which I don't know what it means.

This is my form load code and I'm trying to have module called BackupOnOpen() execute when the user opens the form. Interestingly if I do something like Call BackupOnOpen() in the form_load section my () after BackupOnOpen disappear when I go to the next line.

CODE
Private Sub Form_Load()

    LblMenuVer.Caption = "Version " & App.Major & "." & App.Minor & "." & App.Revision
    
'   Open Database Connection
    Set cnims13 = New ADODB.Connection
    cnims13.Open MSJetVersion & _
        "Data Source=" & strPathDatabase & strDatabaseName & ";"

End Sub


Thanks
Go to the top of the page
 
Start new topic
Replies
theDBguy
post Jan 13 2017, 10:49 AM
Post#2


UA Moderator
Posts: 78,097
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Can you post the code for your module? Thanks.
Go to the top of the page
 
JV63
post Jan 13 2017, 10:58 AM
Post#3



Posts: 197
Joined: 16-August 12



Sure thing. Thanks

This works well with Access as module when I place it in AutoExec but this time trying to do it through strictly a form created in VB6.

CODE
Option Explicit
' This  function copies the Access database to a backup
' location when the database is opened.
' It does this only once in any given day.

Public Function BackupOnOpen()

' ***  CHANGE THE FOLLOWING LINE TO MATCH YOUR BACKUP DESTINATION
' Ensure you have a \ on the end of the pathname.
Const BACKUP_PATH = "\\cdtvnas13\pricingtool\"

On Error GoTo BackupOnOpen_Err

If DCount("BackupDate", "tblBackupDetails", "BackupDate = date()") <> 0 Then
Exit Function
End If

Dim strSourcePath As String
Dim strSourceFile As String
Dim strBackupFile As String
'strSourcePath = GetFileName(CurrentDb.NAME, False)   '  false means we want pathname
'strSourceFile = GetFileName(CurrentDb.NAME, True)    '  true means we want filename
strSourcePath = GetFileName("\\plyvnas02\ops\priceconversion\pricingtool\", False)   '  false means we want pathname
strSourceFile = GetFileName("\\plyvnas02\ops\priceconversion\pricingtool\pricetest.accdb", True)    '  true means we want filename
strBackupFile = "BackupDB-" & Format(Date, "yyyy-mm-dd") _
& "_" & Format(Time, "hhmmss") & "-" & strSourceFile

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile strSourcePath & strSourceFile, BACKUP_PATH & strBackupFile, True
Set fso = Nothing
DoCmd.SetWarnings False
Dim SQL As String
SQL = "INSERT INTO tblBackupDetails " _
  & "(BackupDate, ComputerName, BackupFolder, Filename) " _
  & "VALUES ('" & Date & "', '" & Environ("COMPUTERNAME") _
  & "', '" & BACKUP_PATH & "', '" & strBackupFile & "');"
DoCmd.RunSQL SQL
SQL = "DELETE * FROM tblBackupDetails WHERE BackupDate < date() - 10;"
DoCmd.RunSQL SQL
DoCmd.SetWarnings True
BackupOnOpen_Exit:
Exit Function
BackupOnOpen_Err:
MsgBox Err.Description, , "BackupOnOpen()"
Resume BackupOnOpen_Exit
End Function
                    
' This  function is given a filename complete with path, and
' a boolean value.
' If the  boolean is true, this function returns only the
' filename (without the path).
' If the  boolean is false, this function returns only the
' path (without the filename).
'  Author:   Mike Perris - mikeperris.com
'  Date:     13-Mar-2011
'  Version:  1.1
' Revision  History:
' Rev       Date(yyyy/mm/dd)        Description
' 1.1       13/3/2011               Added the boolean bit and  extended code to
'                                   extract path  as well as filename.
Function GetFileName(FullPath As String, IsFile As Boolean)
Dim icount  As Integer
icount = Len(FullPath)
Do Until Mid(FullPath, icount, 1) = "\"
icount = icount - 1
Loop

If IsFile Then
GetFileName = Right(FullPath, Len(FullPath) - icount)
Else
GetFileName = Left(FullPath, icount)
End If
End Function
Go to the top of the page
 
theDBguy
post Jan 14 2017, 11:22 AM
Post#4


UA Moderator
Posts: 78,097
Joined: 19-June 07
From: SunnySandyEggo


Thanks. Which line gets highlighted when you get the error?
Go to the top of the page
 
JV63
post Jan 16 2017, 07:53 AM
Post#5



Posts: 197
Joined: 16-August 12



Trying it as a basic call, when I get the "reserved error" the call is highlighted.

CODE
Private Sub Form_Load()

    LblMenuVer.Caption = "Version " & App.Major & "." & App.Minor & "." & App.Revision
    
    Call BackupOnOpen
    
'   Open Database Connection
    Set cnims13 = New ADODB.Connection
    cnims13.Open MSJetVersion & _
        "Data Source=" & strPathDatabase & strDatabaseName & ";"

End Sub
Go to the top of the page
 

Posts in this topic
- JV63   Executing A Module On Form Load?   Jan 13 2017, 09:32 AM
- - theDBguy   Hi, Can you post the code for your module? Thanks...   Jan 13 2017, 10:49 AM
- - JV63   Sure thing. Thanks This works well with Access as...   Jan 13 2017, 10:58 AM
- - theDBguy   Thanks. Which line gets highlighted when you get t...   Jan 14 2017, 11:22 AM
- - JV63   Trying it as a basic call, when I get the "re...   Jan 16 2017, 07:53 AM
- - theDBguy   Do you still get an error if you don't use ...   Jan 16 2017, 10:25 AM
- - JV63   No, but then that new module doesn't execute i...   Jan 16 2017, 10:28 AM
- - theDBguy   Hi, Sorry for the confusion... What I meant was t...   Jan 16 2017, 10:31 AM
- - JV63   If I run it like this, I get reserved error still....   Jan 16 2017, 10:44 AM
- - theDBguy   If you hit Ctrl+G and then enter BackupOnOpen Do...   Jan 16 2017, 11:35 AM
- - JV63   Yes, reserved error.   Jan 16 2017, 11:46 AM
- - theDBguy   Okay, this eliminates your first procedure, which ...   Jan 16 2017, 11:51 AM
- - JV63   Yes. If I compile it without doing anything with t...   Jan 16 2017, 11:55 AM
- - theDBguy   Okay, one last thing... Have you tried stepping th...   Jan 16 2017, 12:01 PM
- - JV63   Doing a step into takes me through the program and...   Jan 16 2017, 12:10 PM
- - theDBguy   Hi, What I meant was something like this: 1. Put...   Jan 16 2017, 12:13 PM
- - JV63   Thanks for clarifying. If I did that correctly I h...   Jan 16 2017, 01:12 PM
- - theDBguy   Hi, At least we're getting somewhere now... ...   Jan 16 2017, 01:20 PM
- - JV63   Ok, not I got the reserved error with this line hi...   Jan 16 2017, 01:28 PM
- - theDBguy   Hi, Now we're getting closer... In the Immed...   Jan 16 2017, 01:34 PM
- - JV63   By putting that into the immediate window I get th...   Jan 16 2017, 01:39 PM
- - theDBguy   Okay, so now, you'll just need to figure out a...   Jan 16 2017, 03:12 PM
- - JV63   I do get an reserved error with that immediate cod...   Jan 16 2017, 03:28 PM
- - theDBguy   Okay thanks. I don't see any reserved words in...   Jan 16 2017, 03:44 PM
- - JV63   Yes, reserved error as well.   Jan 16 2017, 03:49 PM
- - theDBguy   Hi, See if you can safely use DCount() against ot...   Jan 16 2017, 04:11 PM
- - JV63   Ok, ,will take a look. the backup table is empty a...   Jan 16 2017, 04:13 PM
- - theDBguy   Even if the table is empty, DCount("*",...   Jan 16 2017, 04:23 PM
- - JV63   By commenting out the whole Dcount I got it to cre...   Jan 16 2017, 04:25 PM
- - theDBguy   Well, first, we'll need to make sure DCount() ...   Jan 16 2017, 04:27 PM
- - JV63   Yeah, something is up with Dcount cause I'm ge...   Jan 16 2017, 04:38 PM
- - theDBguy   Were you missing any references?   Jan 16 2017, 05:29 PM
- - JV63   Not that I'm aware of. I'll have to look d...   Jan 16 2017, 05:41 PM
- - theDBguy   Hi, If you get stuck, please consider posting a s...   Jan 16 2017, 07:16 PM



Custom Search


RSSSearch   Top   Lo-Fi    26th May 2020 - 01:44 PM