1 year ago

#333020

test-img

Prattdakota

Need to capture picture from webcam on Surface tablet using VBA in Access 365

I am working on a project that requires the ability to capture pictures on a tablet from the device webcam. I am using a Surface Go with Access 365. My application is complex enough to require the use of VBA.

To accomplish the picture capture part of this project I used the VBA code below which I took from a previous SO post (https://stackoverflow.com/questions/56757965/how-to-use-webcam-capture-on-a-microsoft-access-form) and then modified.

I built my Access database application using my desktop computer under the same Office 365 account I use on my Surface Go. It works terrific under those circumstances. When I open the application in my OneDrive folder using the Surface Go the database comes up fine and works well until I try to start the webcam (Cmd1 subroutine).

When the Cmd1 subroutine is called to start the webcam a dialog comes up asking to Select a Video Device and the choices are either Microsoft Camera Front or Microsoft Camera Rear. After selecting one of the choices I can click OK, Cancel or Apply. Apply leaves the dialog open so Ok or Cancel must be selected. I've tried OK and Apply -> OK. I would expect the image from the webcam to appear but instead the PicWebCam control remains blank (that is black). Also, I've tried selecting both Front and Rear while testing.

I should note that the Cmd1 subroutine completes without errors. If I use the button to Save Image, it will in fact create a .JPG file. The file however appears to be corrupt in some way as Windows OS can't seem to read it. Since there's no image displayed initially, this is of secondary concern and may be related to the fact that an image is not displayed.

In learning about the avicap32.dll, there is some thought that this library is a bit outdated or at least has been around for quite some time. Certainly, given the webcam hardware on the Surface Go is different than the webcam attached to my desktop, the ability of this .dll to work with the webcam could be an issue. I'm getting a bit out of my league here.

It seems the technology must exist to capture the webcam image. I can open the Excel Mobile app on my Surface Go, open a document, goto Insert -> Pictures -> Camera and it brings up the webcam just fine. I can snap a picture and it puts it right in my document.

I'd certainly appreciate any thoughts you might have on how to correct this or an alternative. Thank you.

Option Compare Database

Public intPictureCount As Integer

Const WS_CHILD As Long = &H40000000
Const WS_VISIBLE As Long = &H10000000

Const WM_USER As Long = &H400
Const WM_CAP_START As Long = WM_USER

Const WM_CAP_DRIVER_CONNECT As Long = WM_CAP_START + 10
Const WM_CAP_DRIVER_DISCONNECT As Long = WM_CAP_START + 11
Const WM_CAP_SET_PREVIEW As Long = WM_CAP_START + 50
Const WM_CAP_SET_PREVIEWRATE As Long = WM_CAP_START + 52
Const WM_CAP_DLG_VIDEOFORMAT As Long = WM_CAP_START + 41
Const WM_CAP_FILE_SAVEDIB As Long = WM_CAP_START + 25

Private Declare PtrSafe Function capCreateCaptureWindow _
    Lib "avicap32.dll" Alias "capCreateCaptureWindowA" _
         (ByVal lpszWindowName As String, ByVal dwStyle As Long _
        , ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long _
        , ByVal nHeight As Long, ByVal hwndParent As LongPtr _
        , ByVal nID As Long) As Long

Private Declare PtrSafe Function SendMessage Lib "user32" _
    Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long _
        , ByVal wParam As Long, ByRef lParam As Any) As Long

Dim hCap As LongPtr

Private Sub cmd4_Click()
'Take Picture

Dim sFileName As String
Dim strOpenArgs As String

'Dim img As Image

    strOpenArgs = Forms!frmCamera.OpenArgs

    Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(False), 0&)

'    sFileName = GetSavePath
    sFileName = DBPath() & strOpenArgs & " - Picture" & intPictureCount & ".jpg"

    Call SendMessage(hCap, WM_CAP_FILE_SAVEDIB, 0&, ByVal CStr(sFileName))
    
'    Forms!frmprojectintake.Image.Picture = sFileName
    
    Select Case intPictureCount
        Case 1
            Forms!frmprojectintake.Image1.Picture = sFileName
        Case 2
            Forms!frmprojectintake.Image2.Picture = sFileName
        Case 3
            Forms!frmprojectintake.Image3.Picture = sFileName
        Case 4
            Forms!frmprojectintake.Image4.Picture = sFileName
        Case 5
            Forms!frmprojectintake.Image5.Picture = sFileName
        Case 6
            Forms!frmprojectintake.Image6.Picture = sFileName
        Case 7
            Forms!frmprojectintake.Image7.Picture = sFileName
        Case 8
            Forms!frmprojectintake.Image8.Picture = sFileName
    End Select
    
    intPictureCount = intPictureCount + 1
    
DoFinally:
    Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
End Sub

Private Sub cmd3_Click()
'Close Webcam

Dim temp As Long
temp = SendMessage(hCap, WM_CAP_DRIVER_DISCONNECT, 0&, 0&)
End Sub


Private Sub cmd1_Click()
'Start Webcam

    intPictureCount = 1

    hCap = capCreateCaptureWindow("Take a Camera Shot", WS_CHILD Or WS_VISIBLE, 0, 0, PicWebCam.Width, PicWebCam.Height, PicWebCam.Form.hWnd, 0)
    If hCap <> 0 Then
        Call SendMessage(hCap, WM_CAP_DRIVER_CONNECT, 0, 0)
        Call SendMessage(hCap, WM_CAP_SET_PREVIEWRATE, 66, 0&)
        Call SendMessage(hCap, WM_CAP_SET_PREVIEW, CLng(True), 0&)
    End If
End Sub

Private Sub cmd2_Click()
'Format Webcam

Dim temp As Long
temp = SendMessage(hCap, WM_CAP_DLG_VIDEOFORMAT, 0&, 0&)
End Sub


Private Sub cmd5_Click()

    DoCmd.Close acForm, "frmCamera"

End Sub

Private Sub Form_Load()
cmd1.Caption = "Start &Cam"
cmd2.Caption = "&Format Cam"
cmd3.Caption = "&Close Cam"
cmd4.Caption = "&Save Image"
cmd5.Caption = "Exit Camera"
End Sub

Function GetSavePath() As String
    Dim f As Object 'FileDialog
    Set f = Application.FileDialog(2) 'msoFileDialogSaveAs
    If f.Show <> 0 Then GetSavePath = f.SelectedItems(1)
End Function

vba

ms-access

webcam

tablet

0 Answers

Your Answer

Accepted video resources