Click here to Skip to main content
15,897,371 members

Comments by ninpo (Top 7 by date)

ninpo 8-Apr-22 12:32pm View    
Apparently it is possible here, but i cannot get this to work in MFC?

https://www.codeproject.com/Articles/24969/An-MFC-picture-control-to-dynamically-show-picture?msg=5870920#xx5870920xx

The comment he has this:

bool CDataDlg::GetImageDataFromDB(unsigned char** lpData, long& nSize, _RecordsetPtr& pRst)
{
//Helper variable for retrieving image data
long lngOffSet = 0;
long lngSize = nSize = pRst->Fields->Item["Data"]->ActualSize;

const long ChunkSize = 50;
_variant_t varChunk;
UCHAR chData;
HRESULT hr;
long lBytesCopied = 0;
*lpData = new unsigned char[lngSize];

//Retrieveing data from vararray
while (lngOffSet < lngSize)
{
try
{
//Get 50 size long chunk from database
//varChunk = pField->GetChunk(ChunkSize);
varChunk = pRst->Fields->Item["Data"]->GetChunk(ChunkSize);


//putting chunk in to safe array
for (long lIndex = 0; lIndex <= (ChunkSize - 1); lIndex++)
{
hr = SafeArrayGetElement(varChunk.parray, &lIndex, &chData);
if (SUCCEEDED(hr))
{
((UCHAR*)(*lpData))[lBytesCopied] = chData;
lBytesCopied++;
}
else
break;
}
lngOffSet += ChunkSize;
}
catch (_com_error &e)
{
CString sBuff = GetDocument()->GetErrorDescription(e);
AfxMessageBox(sBuff);
return false;
}
}

return true;
}
ninpo 8-Apr-22 11:13am View    
What about Exporting the VBA function as a API?
So if I make the VBA exportAttachment (long index)


Then in MFC,C++
Import the module exportAttachment (long index)

And use it?


void CDlg::OnBnClickedButtonGetAttachments()
{
long nRef=1;
long uReturnVal=-1;

long (*lpfnDllFunc1) (long ) = NULL;
// HINSTANCE hDLL; // Handle to DLL

HMODULE hDLL;
hDLL=LoadLibrary(_T("Access Database? or ?.dll"));

// hDLL=AfxLoadLibrary(_T("ToolLib.dll"));
if(hDLL != NULL)
{

lpfnDllFunc1 = (long (*)(long))GetProcAddress(hDLL,"VBA_ExportAttachment");


if (!lpfnDllFunc1)//
{
// handle the error
FreeLibrary(hDLL);
// AfxFreeLibrary(hDLL);
AfxMessageBox(_T("error loading function"));
}
else
{
// call the function ( just want the dlg to pop up)
uReturnVal = lpfnDllFunc1(nRef,);
if(uReturnVal==0)
AfxMessageBox(_T(" failed"));
}
}
FreeLibrary(hDLL);
// AfxFreeLibrary(hDLL);

}
ninpo 8-Apr-22 10:58am View    
Excellent, how to do that?

Because it "appears" that you cannot get C++/MFC to export attachments in a Microsoft database.
ninpo 8-Apr-22 10:20am View    
Also if I do succeed in doing from a vba standpoint, is it possible to call that module from C++/MFC Application?
ninpo 8-Apr-22 10:17am View    
OK Yes I understand that, But, how to get the file or the address or extract it from Access??? I tried the VBA code in Access and that didn't work, it did not extract the files.

Some code or something is what I would like to see.


------------ VBA Code ----------------

Option Compare Database

Sub exportAttachments()

Dim strPath, fName, fldName, sName(3) As String
Dim rsPictures, rsDes As Variant
Dim rs As DAO.Recordset
Dim savedFile, i As Integer
savedFile = 0

strPath = Application.CurrentProject.Path

Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblTools")

'Check to see if the recordset actually contains rows
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst 'Not required here, but still a good habit
Do Until rs.EOF = True
On Error Resume Next 'ignore errors

'Instantiate the child record set.
Set rsPictures = rs.Fields("Picture").Value
Set rsDes = rs.Fields("Name") 'use to name the picture later

'if no attachment available, go to next record
If Len(rsPictures.Fields("FileName")) = 0 Then
GoTo nextRS
End If
If rsPictures.RecordCount <> 0 Then
rsPictures.MoveLast
savedFile = rsPictures.RecordCount 'set savedFile = total no of attachments
End If
rsPictures.MoveFirst ' move to first attachment file

'WARNING: all of my attachments are picture with JPG extension.
'loop through all attachments
For i = 1 To savedFile 'rename all files and save
If Not rsPictures.EOF Then
fName = strPath & "\\Attachments\\" & rsDes & i & ".JPG"
rsPictures.Fields("FileData").SaveToFile fName
sName(i) = fName 'keep path in an array for later use
rsPictures.MoveNext
End If
Next i

'insert image name and path into database an edit
' rs.Edit

' If Len(sName(1)) <> 0 Then
' rs!PicPath1 = CStr(sName(1)) 'path
' rs!PicDes1 = Left(Dir(sName(1)), InStr(1, Dir(sName(1)), ".") - 1) 'file name without extension
' End If
' If Len(sName(2)) <> 0 Then
' rs!PicPath2 = CStr(sName(2))
' rs!PicDes2 = Left(Dir(sName(2)), InStr(1, Dir(sName(2)), ".") - 1)
' End If
' If Len(sName(3)) <> 0 Then
' rs!PicPath3 = CStr(sName(3))
' rs!PicDes3 = Left(Dir(sName(3)), InStr(1, Dir(sName(3)), ".") - 1)
' End If

' rs.Update 'update record
nextRS:
rsPictures.Close 'close attachment
savedFile = 0 'reset for next
fName = 0 'reset

'Move to the next record.
rs.MoveNext
Loop

Else
MsgBox "There are no records in the recordset."
End If

MsgBox "Attachments were exported!"

rs.Close 'Close the db recordsets
Set rs = Nothing 'Clean up

End Sub