19

How do I debug a bad DLL calling convention error in MSAccess VBA code?

I made some changes to a function in a module and then got the error. How do a debug it to find the cause?

The error occurs on the Exit function statement of the function.

braX
  • 11,506
  • 5
  • 20
  • 33
Malcolm
  • 12,524
  • 28
  • 89
  • 125
  • You're calling a native function declared with the "Declare" statement, right? Could you share both the declaration and the call with us? – Dan Byström Feb 22 '09 at 08:56
  • 3
    I fixed it...all i had to do is start access with /decompile and then recompile and the error went away. – Malcolm Feb 22 '09 at 11:55
  • For resolutions to this error in Excel, see my post at: [Runtime Error 49, Bad DLL calling convention][1] [1]: http://stackoverflow.com/questions/15758834/runtime-error-49-bad-dll-calling-convention – pstraton Sep 20 '14 at 00:28
  • This worked for me! https://stackoverflow.com/a/36720241/4836581 – Zvi Redler Mar 21 '23 at 10:14

6 Answers6

30

I fixed this problem by making a change in one of my class modules, executing a compilation from the Debug menu, and then undoing the change.

Background: In my case I've seen this in Excel without any external references. It happened, as with your problem, on an Exit Function call. Excel doesn't seem to have a /decompile option. I suspect that one of my class modules had mis-compiled for some reason, and Excel won't re-compile unless it thinks something has changed.

ndemou
  • 4,691
  • 2
  • 30
  • 33
Jon Artus
  • 6,268
  • 11
  • 42
  • 41
  • 10
    Holy f* thanks for posting this, exact problem I had. I just placed an xxxx at the top of every module, then: Debug, Compile VBAProject (which will highlight a xxxx)...delete that --> Debug, Compile VBAProject...repeat until no more compile errors. – tbone Jun 25 '12 at 23:18
  • Sometimes a small change is not enough, because VBA does an incremental compilation and the small changes may not trigger the re-compilation of the corrupted area. A select all/cut/paste on each module usually does the trick – stenci Jan 16 '20 at 23:27
  • @Jon-artus I rearanged your sentences. I believe that it highlights the fix better but if you feel otherwise don't hesitate to revert – ndemou Nov 05 '22 at 16:57
20

Have you checked your references and decompiled?

"C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" 
                                "d:\My Documents\access\mayapp.mdb" /decompile

See also:
http://www.granite.ab.ca/access/decompile.htm
VBScript Decompile


Check references in code

Dim ref As Reference
Dim sMsg As String

''Available since 2010
If BrokenReference Then
    For Each ref In References
        ''Available since at least 2000
        If ref.IsBroken Then
            sMsg = sMsg & "Ref Name: " & ref.Name
            'Also, if required
            'sMsg = sMsg & vbCrLf & "Built In: " & ref.BuiltIn
            'sMsg = sMsg & vbCrLf & "Full Path: " & ref.FullPath
            'sMsg = sMsg & vbCrLf & "GUID: " & ref.Guid
            'sMsg = sMsg & vbCrLf & "Kind: " & ref.Kind
            'sMsg = sMsg & vbCrLf & "Major (version number): " & ref.Major
            'sMsg = sMsg & vbCrLf & "Minor (version number): " & ref.Minor
            sMsg = sMsg & vbCrLf & "=================================" & vbCrLf
        End If
    Next
    MsgBox sMsg
End If
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • I ran into this error on a call (and again on return from function) to an Access VBA function I'd written - not an external API function. /decompile and recompile did the trick. I suspect that a lot of debugging (rather than just running) a recursive function got Access confused, and /decompile cleaned up the mess. – SebTHU Dec 19 '15 at 13:42
3

I experienced and worked around this error using the .NET library for WinSCP from MS Access VBA.

What happened was:

  1. A function UploadSomething for connecting to an SFTP server and uploading a file worked fine.
  2. Within the function UploadSomething changed the "resume support" option with this code: myTransferOptions.ResumeSupport.State = TransferResumeSupportState.TransferResumeSupportState_Off

After the change, the code worked as desired. However in the code that called UploadSomething, Error 49 was thrown after the function had finished.

The error happened both when stepping through the code using the debugger and when executing at once outside of the debugger. Recompiling the project didn't work for me.

What did work was this:

  1. Remove the reference to the COM component
  2. Add the reference to the COM component
  3. Recompile
AronVanAmmers
  • 1,668
  • 20
  • 24
1

In Excel VBA, this can be caused by any of several problems:

  1. A parameter or return-value type mismatch.
  2. An object method (such as AutoFit) applied to an erroneous object variation for which that method isn’t available.
  3. A call to an external library function.
  4. Broken library references

For resolutions to these causes, see my post at: Runtime Error 49, Bad DLL calling convention

Community
  • 1
  • 1
pstraton
  • 1,080
  • 14
  • 9
0

I've just got this in Excel and wondered if anyone else have gotten it previously. My solution was to move around the references to my own DLL and click 'Compile <Project>'.

graham.reeds
  • 16,230
  • 17
  • 74
  • 137
0

We've run into some problems with VBA when trying to call a DLL compiled in Intel Fortran. It turns out that you need to align the calling conventions back to a "C" context with the compiler flag calling convention: cfv

More info here on the Intel website Another useful thread on the same problem: Intel Fortran DLL <-> C

RexBarker
  • 1,456
  • 16
  • 14