473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Sub or Function not defined

Seth Schrock
2,965 Expert 2GB
I am trying to compile my VBA code in Access 2010, however, I keep getting this error: Sub or Function not defined. When I click OK, it highlights the following code:
Expand|Select|Wrap|Line Numbers
  1. Me.txtFileLoc
  2.  
  3. 'Full code line
  4. If Me.txtFileLoc & "" <> "" And FileExists(Me.txtFileLoc) Then
I know that the spelling is correct because I used the inteliSense prompt to finish the typing for me. I tried renaming the control, same problem (after I renamed the control reference of course). I tried referencing it with the long reference through the form like this:
Expand|Select|Wrap|Line Numbers
  1. Forms("frmFileImport").Controls("txtFileLoc")
The error that I get then highlights the .Controls. Further up the form's code module, I have a reference to the same textbox and it doesn't have a problem with it. I discovered it because when I renamed the control and tried to compile the database, it then caught it. Once I fixed the name, it worked and then went back down to this same problem line. I have compacted and repaired the database. I have rebooted my computer. I tried removing my reference to the Microsoft Office xx.x Object Library that I had added and still nothing (I didn't think that it would, but I had to try). I don't know what else to do at this point. Any ideas?
Apr 4 '14 #1
9 6270
Seth Schrock
2,965 Expert 2GB
I think that I just fixed it, but I have no idea how it fixed it. I switched the order of the criteria in line #4 and it works now. So my code is now
Expand|Select|Wrap|Line Numbers
  1. If FileExists(Me.txtFileLoc) And Me.txtFileLoc & "" <> "" Then
Can anyone tell me why this made a difference?
Apr 4 '14 #2
NeoPa
32,556 Expert Mod 16PB
I can only imagine the issue is somewhere other than the code you've shown Seth. The compiler often gets confused when paired statements are not paired (Like Do / Loop, For / Next, If / End If, etc). It seems to report the issue as something further down the code.

If all you say is fully accurate then I cannot see why it would report it as you've said.
Apr 6 '14 #3
Seth Schrock
2,965 Expert 2GB
Here is the full procedure in its current state. The only thing that got changed was the If statement on line 7 (line 20 of the incode line numbers)
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdImport_Click()
  2. 10    On Error GoTo Error_Handler
  3.  
  4.       Dim PF As TSC_PF_Simple
  5.       Dim strFilePath() As String
  6.  
  7. 20    If FileExists(Me.txtFileLoc) And Me.txtFileLoc & "" <> "" Then
  8. 30        If Not IsNull(Me.cboScanDate) And Not IsNull(Me.cboBranch) Then
  9. 40            Set PF = New TSC_PF_Simple
  10.  
  11. 50            PF.Title = "Importing Scan Results..."
  12. 60            PF.UpdateTask 0, "Preparing for Import"
  13. 70            PF.Show
  14.  
  15. 80            DoCmd.SetWarnings False
  16. 90            DoCmd.RunSQL "DELETE FROM VulnerabilitiesImport"
  17. 100           DoCmd.SetWarnings True
  18.  
  19. 110           strFilePath = Split(Me.txtFileLoc, "\")
  20.  
  21. 120           PF.UpdateTask 0.05, "Importing File: " & strFilePath(UBound(strFilePath))
  22. 130           DoCmd.TransferText acImportDelim, , "VulnerabilitiesImport", Me.txtFileLoc, True
  23.  
  24.               'Run the information setup Files
  25. 140           PF.UpdateTask 0.5, "Processing Risk Levels"
  26. 150           InsertRiskLevels
  27.  
  28. 160           PF.UpdateTask 0.6, "Processing Hosts"
  29. 170           InsertHosts Me.cboBranch
  30.  
  31. 180           PF.UpdateTask 0.7, "Processing Vulnerabilities"
  32. 190           InsertVulnerabilities
  33.  
  34. 200           PF.UpdateTask 0.8, "Merging Data"
  35. 210           CopyVulnerabilities Me.cboScanDate
  36.  
  37. 220           PF.Title = "Import Complete"
  38. 230           PF.UpdateTask 1, "Operation Successful"
  39.  
  40. 240       Else
  41. 250           MsgBox "Please select a scan description and branch"
  42. 260       End If
  43. 270   Else
  44. 280       MsgBox "Either no file has been selected or the selected file does not exist.  Please select an existing file."
  45. 290   End If
  46.  
  47. Exit_Procedure:
  48. 300       Set PF = Nothing
  49. 310       Exit Sub
  50.  
  51. Error_Handler:
  52. 320       If Err.Number = 3501 Then
  53. 330           MsgBox "The file you have selected is open.  Please close it and then try importing it again."
  54. 340           Resume Exit_Procedure
  55. 350       End If
  56.  
  57. 360       If TempVars("RunMode") = "Test" Then
  58. 370           Call ErrorMessage(Err.Number, Err.Description, "Form_frmFileImport: cmdImport_Click")
  59. 380       Else
  60. 390           TSCs_ReportUnexpectedError "cmdImport_Click", "Form_frmFileImport", "Custom info"
  61. 400       End If
  62. 410       Resume Exit_Procedure
  63. 420       Resume
  64.  
  65.  
  66. End Sub
  67.  
Apr 7 '14 #4
jimatqsi
1,271 Expert 1GB
I think the lack of parenthesis in the complex IF / THEN has contributed to this problem. When in doubt, and especially when not in doubt, use ()

Jim
Apr 7 '14 #5
Seth Schrock
2,965 Expert 2GB
I didn't think of using parenthesis. That might explain why I had the issue. Unfortunately, I can't duplicate the error by just switching the order back to how it was since it doesn't produce the error any more. Maybe it just figured out what I was trying to do :)
Apr 7 '14 #6
NeoPa
32,556 Expert Mod 16PB
Although it can often make the code easier to read when parentheses (plural) are added, it's rarely necessary. This is because all operators have a level of precedence which usually ensures they are processed in the order you would expect.

Personally, I'd code it as :
Expand|Select|Wrap|Line Numbers
  1. With Me
  2.     If .txtFileLoc > "" Then
  3.         If FileExists(.txtFileLoc) Then
  4.             ...
  5.         End If
  6.     End If
  7. End With
But that's not because your way wouldn't be expected to work fine TBF.
Apr 8 '14 #7
jimatqsi
1,271 Expert 1GB
I like NeoPa's way, it adds clarity at the cost of nothing.

Jim
Apr 8 '14 #8
Seth Schrock
2,965 Expert 2GB
For some reason I hate nesting IF statements. Two I'm okay with, but after that, I try to figure out another way. I guess that is why I do most of mine in a single IF statement when possible. However, I will take the recommendation of an expert over my self-taught practices any day. Thanks for all your help.
Apr 8 '14 #9
NeoPa
32,556 Expert Mod 16PB
Normally I'd use And and Or within a single statement but in this case one depends on the other, so it fits better to test one first then only even check the other if it's worth it. When using And and Or it's easier to read and maintain if parentheses are used.
Apr 8 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Andrew Boothman | last post by:
Hi, This may be the dumbest question ever, but in the following code (using PHP 4.3.8) how do I call b() from within a()? class test { function a() { print "a called"; b();
3
by: lallous | last post by:
Usually you can check for a defined variable as: #ifdef DEF1 // do stuff here #endif Can I test if a function is defined then do things, example: int myFnc() { // blah blah }
5
by: rashmi | last post by:
how to use static function defined in one file in another file is that impposiible in 'c '
12
by: jeniffer | last post by:
Can a static function defined in a C file be ever referred (called) externally from another C file?If so in which conditions?
19
by: thisis | last post by:
Hi All, i have this.asp page: <script type="text/vbscript"> Function myFunc(val1ok, val2ok) ' do something ok myFunc = " return something ok" End Function </script>
6
by: silverburgh.meryl | last post by:
Hi, I have a function called 'test' defined in A.py. How can I call that function test in my another file B.py? Thank you.
2
by: yawnmoth | last post by:
Say I have the following: <script> var cat = (function() { var name = ""; function changeName(name) { this.name = name; }
22
by: thekingsnake | last post by:
After following the instructions from the answer below: http://bytes.com/topic/javascript/answers/153274-script-iframe-can-not-call-functions-defined-parent-document I was able to have the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.