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

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Compile Error - Variable Not Defined    
 
   
JV63
post Oct 12 2018, 02:15 PM
Post#1



Posts: 194
Joined: 16-August 12



Hi. I have a piece of code that is giving a compile error. The message says it's not defined but the odd thing is I have a different project with similar code and the same variable (FTP1) that is not defined either and it runs fine. Not sure what I'm missing. If I try to declare it, I'm not sure of the type. I inherited a bunch of VB6 project code that I'm trying to get to work similar to the one that does work. I can't just copy one to the other as there are some differences elsewhere.

This is part of a project that opens a user interface to import data into an Access database.

I get the error on the code below and it highlights Ftp1. Looking for ideas of where else to look.

Thanks

CODE
Private Sub cbCancel_Click()
    If Ftp1.State = tcpConnected Then
        If bRetrieving Then
            Ftp1.Abort
        End If
        Ftp1.Logout
    End If
    If Timer1.Enabled = True Then
        Timecount = 99
    Else
        FTPrc = 8
        Unload Me
    End If
End Sub
Go to the top of the page
 
theDBguy
post Oct 12 2018, 02:21 PM
Post#2


Access Wiki and Forums Moderator
Posts: 73,956
Joined: 19-June 07
From: SunnySandyEggo


Hi,

Re: "Not sure what I'm missing."

You're probably missing Option Explicit in the one where the code works. How is the variable FTP1 being set or instantiated?

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
JV63
post Oct 12 2018, 02:25 PM
Post#3



Posts: 194
Joined: 16-August 12



Not sure how it's set but in both instances there is Option Explicit.

Here's some more of the code. Same for one that works and one that doesn't.

CODE
Option Explicit
Dim gAppName As String
Dim gHost As String
Dim gUser As String
Dim gPass As String
Dim gRemoteFileName As String
Dim gLocalFileName As String
Dim bRetrieving As Boolean
Dim Timecount As Integer

Private Sub cbCancel_Click()
    If Ftp1.State = tcpConnected Then
        If bRetrieving Then
            Ftp1.Abort
        End If
        Ftp1.Logout
    End If
    If Timer1.Enabled = True Then
        Timecount = 99
    Else
        FTPrc = 8
        Unload Me
    End If
End Sub

This post has been edited by JV63: Oct 12 2018, 02:25 PM
Go to the top of the page
 
theDBguy
post Oct 12 2018, 03:05 PM
Post#4


Access Wiki and Forums Moderator
Posts: 73,956
Joined: 19-June 07
From: SunnySandyEggo


Hi,

How many standard or class modules do you have in each file? Are you sure they're all the same in both files? If not, then search for all instances of FTP1 in the working file and see if you have the same exact ones in the non-working file.

It's possible the variable was declared with a global scope in the working version.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
GroverParkGeorge
post Oct 12 2018, 05:27 PM
Post#5


UA Admin
Posts: 34,136
Joined: 20-June 02
From: Newcastle, WA


Further to theDBguy's explanation. Variable Scope in VBA. Although that article refers to Excel, the concepts apply to VBA in Access.

--------------------
My Real Name Is George. Grover Park Consulting is where I do business.
How to Ask a Good Question
Beginning SQL Server
Visit My Blog on Facebook
Go to the top of the page
 
AlbertKallal
post Oct 12 2018, 07:23 PM
Post#6


UtterAccess VIP
Posts: 2,705
Joined: 12-April 07
From: Edmonton, Alberta Canada


There are 3 possible choices.


In the past, you did not have option explicit, so the code compiled (however you HAVE confirmed this).


In the past you had some module with a GLOBAL instance of the class declared, and you are missing that code and declare.
(This is possible, but if you sure this is NOT the case?

Well, then all that is left here is THIS case, and I am betting this is your issue:

In Access 97 (and before), it was possible to use a class module WITHOUT declaring an instance of that class.

And for reasons of compatibility and code not breaking?

Well, for VBA they wanted to adopt the correct object approach and concept that you have to create an instance of a class before you use it. However, lots of code would break!!!

So they added a hidden attribute to VBA code modules that controls this option.

If you “import” the code module from an older application, (say access 97), then that attribute will ALSO move along for the ride. (Actually, I think you have to upgrade????).

This means, that code could have moved from 97 to 2000, then 2003 and so on for a very long time.

Now, if you say do a cut + paste of code or use some kind of import that does NOT copy that special settings, then your code will now NOT compile were as before it would.

So, after access 97, if you create a class module, then you cannot just use:

ClassModuleName.PropertyOrMethod.

And above is exactly what you have.


Two solutions:

In a standard code module (not class), simply declare a instance of the object.

So in some module, do this:

CODE
Option Compare Database
Option Explicit


Public Ftp1 As New Ftp1


Now read this next sentence VERY careful:

If the name of Ftp1 is NOT the same as the class name, then this bug or issue I am speaking of does NOT apply to you. (re-read this sentence until you are 100%, if not 200% crystal clear as to what I am telling you).

And if you for some reason do NOT want to do the above for a fix?

Fix #2: (again assuming this is your issue – and you read that above pargarigm 10 times – do NOT do the following if this is not your case).

Change the setting in VBA that allows you to directly use a class module without creating a instance of the module.

This will involve using the saveas text commad.

When you export your class to a text file, you see this setting:

CODE
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False  
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False    <--- change to True!!!
Option Compare Database
Option Explicit

Dim m_FirstName      As String\


So for code in pre-access 2000, that above setting (VB_Creatble = True) is true.

For 2000 and after, the setting is as above (false).

I can’t quite recall the rules as to when the above attribute DOES follow. I believe if you use access to upgrade, the attribute setting is un-touched.

However, if you import, I think it is changed. So the feature is seemless for when you open the verison in a newer version of Access. But importing will break this.

Anyway, it been so long I don’t care much to worry as to “which” case cause this.

So a saveasText command from the immediate window, edit the above with a text editor (notepad), then delete your class, and re-mport the above with loadfromText.

The result will now that you can in VBA code use the class without declaring a instance of the class and your code will compile just fine.

However, I think the most easy approach is to simply declare the ftp1 public and NOT forget the “new” keyword.

It is a easy dead ringer to tell if this is the case if in fact the class module is also named ftp1.

If it is not, then you are simply missing the declare, and the declare as public suggestion will also fix this case. (and this information to change the setting will NOT work).

Regards,
Albert D. Kallal (Access MVP, 2003-2017)
Edmonton, Alberta Canada
Go to the top of the page
 
cheekybuddha
post Oct 13 2018, 07:19 AM
Post#7


UtterAccess VIP
Posts: 10,649
Joined: 6-December 03
From: Telegraph Hill


@Albert, note that his is in VB6, not Access (aleadt I **think** so because of the forum!)

@JV63,

Can you confirm this is in VB6 please.

Do you have an FTP class module?

Looks like you're also missing a module instantiating the FTP class and also the Timer object too.

--------------------


Regards,

David Marten
Go to the top of the page
 
AlbertKallal
post Oct 13 2018, 08:07 AM
Post#8


UtterAccess VIP
Posts: 2,705
Joined: 12-April 07
From: Edmonton, Alberta Canada


@David

Thanks - this may well indeed be vb6 - but that also has this attribute setting!

(poster should clear up if this is VB6).

However, that same setting and feature is available in VB6 - I don't have VB6 installed on this machine as I type - and can't recall how you "display" those attributes.


What this means is that everything I stated would apply to VB6 (previous versions also allowed use of a class without creating a instance).
(the only difference is that save as text is not required).

So while the save-as text is not required, if the name of variable is the same name as the class module, then a good chance that this is/was still the issue.

I mean, I assume that the class ftp exists in the project.

Edit: as a fyi, the attribute setting is Attribute VB_PredeclaredId = True, not as per above.

R
Albert
Go to the top of the page
 
JV63
post Oct 13 2018, 02:11 PM
Post#9



Posts: 194
Joined: 16-August 12



Thank you for all the replies. This is VB6.

This is the only module being used between the working version and non working and both have the same code.

CODE
Option Explicit
Global FTPrc As Integer
Public Data1 As Database
Public SupplierDBName As String
Public SourceFileName As String
Public LogFileName As String
Public sMyUserID As String
Public sDatahold(200) As String

Public Declare Function GetUserName Lib "advapi32.dll" _
    Alias "GetUserNameA" (ByVal lpBuffer As String, _
    nSize As Long) As Long

Public Declare Function sendMessageByString& Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, _
     ByVal lParam As String)
Go to the top of the page
 
AlbertKallal
post Oct 13 2018, 04:01 PM
Post#10


UtterAccess VIP
Posts: 2,705
Joined: 12-April 07
From: Edmonton, Alberta Canada


Well, there has to be a reference to ftp1.

It not clear I this is an ActiveX component that was dropped into the form, or that you have a class code module called ftp1.

Or you might have a reference to an external library.

So check the references in the older project.


If there is no class module called ftp1 or ftp in the old application, then it likely that the ftp1 is an ActiveX component that was dropped into that given form.

You can put your cursor on ftp1.State and hit shift-f2 to see what occurs (in the working application).

As this point, my best guess is thus that ftp1 is an ActiveX (ftp component) that was dropped into the form from the toolbox in the older application.

So check the references in the older application, it will show this external library.

Right now, so far, this is looking to be that a ActiveX FTP object was dragged onto the form that works in the older application.

Regards,
Albert D. Kallal (Access MVP, 2003-2017)
Edmonton, Alberta Canada

Go to the top of the page
 
JV63
post Oct 15 2018, 07:11 AM
Post#11



Posts: 194
Joined: 16-August 12



Morning,

Making some headway. Got past the FTP1 error by referencing an active x component. Wasn't referenced in the working version so something is different between the two but still got the FTP form to come up and work. Now I'm getting "Database open error". The database is now a new version, .accdb vs. the old .mdb, and I changed the reference from Microsoft DAO 3.6 Object Library to Microsoft Office 16.0 Access database engine object. Perhaps that's not the correct reference?

This is the piece of code where I changed the database extension to the newer version.

CODE
ServerName = "\\cdtvnas13\Ops"
    dbname = "SUP3.[b]accdb[/b]"

Thanks
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    18th December 2018 - 05:45 AM