• Calculating a file's MD5 hash/checksum in VBA?

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Calculating a file's MD5 hash/checksum in VBA?

    Author
    Topic
    #457909

    Hi,
    I would like to calculate a file’s MD5 hash/checksum using a VBA procedure in MS Access without needing a DLL or other library (which I would have to distribute with the database, and I would like to keep it as simple for the users as possible)…
    Is this possible?
    Hasse

    Viewing 0 reply threads
    Author
    Replies
    • #1149552

      Does MD5 for Visual Basic (VB) do what you want?

      • #1149633

        Does MD5 for Visual Basic (VB) do what you want?

        Thanks Hans! Yes it does .

        Now… next problem: unfortunately it’s too slow for large files, yields an error with a ‘regular’ file,…

        So (before spending time figuring out workarounds & debugging) I tried using the Shell function & a small freeware MD5 generator of DiamondCS (page includes screenshot).
        I put this generator in the same folder along with my database… and ‘run’ it with these lines (in order to yield the hash for a file called plan.htm which resides in the same folder too):

        Code:
        Private Sub MD5_Click()
        Dim str As String
        str = Shell(CurrentProject.Path & "md5.exe " & CurrentProject.Path & "plan.htm")
        MsgBox str
        end Sub

        The MgsBox returns a (every time different) number instead of the hash string.
        Is my Shell command wrong? Or is there something else wrong here? (Or should I contact the creator of this tool?)
        I feel pretty much like a greeny…
        Hasse

        • #1149635

          The result of Shell is not the return value of the application that you start, but its TaskID, i.e. the number that Windows uses to identify it.
          You could write the return value to a text file, then open the text file. The following is air code:

          Dim f As Integer
          Dim strLine As String
          Shell “cmd /c ” & Chr(34) & CurrentProject.Path & “md5.exe ” & _
          CurrentProject.Path & “plan.htm > ” & CurrentProject.Path& “temp.txt” & Chr(34)
          f = FreeFile
          Open CurrentProject.Path& “temp.txt” For Input As #f
          Line Input #f, strLine
          Close #f
          Kill CurrentProject.Path& “temp.txt”
          ‘ strLine should now contain the MD5 value

          • #1149806

            Thanks Hans. I tried it but the ‘open’ line yielded an error 53 (can’t find file), also when I for example created such a file myself before running the procedure. I’ll ask a collegue at work if he can fix it and let you know.

    Viewing 0 reply threads
    Reply To: Calculating a file's MD5 hash/checksum in VBA?

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: