Option Compare Database
Private Sub cmbuser_AfterUpdate() ' this is a combobox filter in the form header
Dim strSql As String
Dim blWasFilterOn As Boolean
' Save the FilterOn state. (It's lost during RecordSource change.)
blWasFilterOn = Me.FilterOn
' Change the RecordSource.
If IsNull(Me.cmbuser) Then
If Me.RecordSource <> "elencoeventiqry" Then
Me.RecordSource = "elencoeventiqry"
End If
Else
strSql = "SELECT elencoeventiqry.*, elenco_eventi_attendance.attendant " & _
"FROM elencoeventiqry INNER JOIN elenco_eventi_attendance ON elencoeventiqry.ID = elenco_eventi_attendance.idevent " & _
"WHERE elenco_eventi_attendance.attendant='" & Me.cmbuser & "' " & _
"ORDER BY id;"
Me.RecordSource = strSql
End If
' Apply the filter again, if it was on.
If blWasFilterOn And Not Me.FilterOn Then
Me.FilterOn = True
End If
DoCmd.GoToRecord , , 3
Me.cmbuser = Me.cmbuser
exit_lab:
Exit Sub
Err_lab:
Resume exit_lab
End Sub
Private Sub cmdmyvisit_Click() ' this is a command button in the form footer
Dim db As Database
Dim qdf As QueryDef
Dim strSql As String
Dim blflag As Boolean
Set db = CurrentDb
If Me.RecordSource = "elencoeventiqry" Then
Me.RecordSource = "SELECT elencoeventiqry.*, elenco_eventi_attendance.attendant FROM elencoeventiqry " & _
"INNER JOIN elenco_eventi_attendance ON elencoeventiqry.ID = elenco_eventi_attendance.idevent;"
blflag = True
End If
Call CreateQry("temp1", Me.RecordSource)
strSql = "TRANSFORM Count(temp1.ID) AS ConteggioDiID " & _
"SELECT temp1.nomecliente, temp1.[event type], temp1.elenco_eventi_attendance.attendant " & _
"FROM temp1 " & _
"WHERE temp1.[Event Date] > (date()-365) And temp1.[Event Type] = 'visit' " & _
"GROUP BY elenco_eventi_attendance.attendant, temp1.nomecliente, temp1.[event type] " & _
"ORDER BY temp1.nomecliente " & _
"PIVOT Format([Event Date],'yyyy-mm');"
Call CreateQry("Visit Summary", strSql)
Call openquery("Visit Summary")
If blflag = True Then
Me.RecordSource = "elencoeventiqry"
Me.Requery
DoCmd.GoToRecord , , 3
End If
Set qdf = Nothing
Set db = Nothing
End Sub
Private Sub customer_AfterUpdate()
Me.txtcustomerorig = Me.customer.Column(1)
End Sub
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.salesman = CurrentUser
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer) ' this is to avoid unwanted changes in a record
Dim strMsg As String
Dim iResponse As Integer
' Specify the message to display.
strMsg = "Do you wish to save the changes?" & Chr(10)
strMsg = strMsg & "Click Yes to Save or No to Discard changes to this record."
' Display the message box.
iResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Save Record?")
' Check the user's response.
If iResponse = vbNo Then
' Undo the change.
DoCmd.RunCommand acCmdUndo
' Cancel the update.
Cancel = True
End If
End Sub
Private Sub Form_Load()
DoCmd.GoToRecord , , acLast
Me.cmbcustomer.SetFocus
End Sub
Private Sub txt_idcl_Click()
On Error GoTo Err_lab
Call openform("clienti", 0, "[idcliente] = " & Me!txt_idcl)
exit_lab:
Exit Sub
Err_lab:
MsgBox Err.Description
Resume exit_lab
End Sub
Private Sub cmddsview_Click()
DoCmd.RunCommand acCmdDatasheetView
End Sub
Private Sub ID_DblClick(Cancel As Integer)
DoCmd.RunCommand (acCmdFormView)
End Sub
Private Sub cmdnewfile_Click() ' this is to add an attachment to a subform
On Error GoTo cmdnewfile_dblclick_err
Dim fdialog As Office.FileDialog
Set fdialog = Application.FileDialog(msoFileDialogFilePicker)
Dim nomefile As String
Dim localfile As String ' nome completo file con percorso
Dim extension As String
Dim db As Database
Dim rst As Recordset2
Dim newid As Integer
Dim fld As Field
Dim tdef As TableDef
Dim strSql As String
Dim doctype As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Choose a file"
.Filters.Clear
If .show Then
nomefile = Right(.SelectedItems(1), Len(.SelectedItems(1)) - Len(.InitialFileName))
localfile = .SelectedItems(1)
extension = Right(localfile, Len(localfile) - InStrRev(localfile, "."))
Else
Exit Sub
End If
End With
Set fdialog = Nothing
Set db = CurrentDb
Set rst = db.OpenRecordset("elencoeventiattach", dbOpenDynaset, dbSeeChanges)
rst.AddNew
rst!idsevento = Me.ID
rst!nomefile = nomefile
rst!extension = extension
rst!usercr = CurrentUser
'rst!typeofdoc = "Bundle"
If modOLEBinary_LoadFromFile(rst!attachfilestream, nomefile) Then
MsgBox "File Written"
Else
MsgBox "Written failed"
End If
rst.Update
rst.Bookmark = rst.LastModified
rst.Close
Me.eventiattach.Requery
Set rst = Nothing
db.Close
Set db = Nothing
cmdnewfile_dblclick_exit:
Exit Sub
cmdnewfile_dblclick_err:
MsgBox Err.Description & " " & Err.Number
Resume cmdnewfile_dblclick_exit
End Sub
Function FilterResults() As Variant ' this is a filter in the form header
Dim strCriteria As String
strCriteria = ""
If Nz(Me.cmbcustomer) <> "" Then
If strCriteria <> "" Then
strCriteria = strCriteria & " AND "
End If
If InStr(Nz(Me.cmbcustomer), "*") = 0 Then
strCriteria = strCriteria & " [nomecliente]='" & Me.cmbcustomer & "'"
Else
strCriteria = strCriteria & " [nomecliente] LIKE '*" & Me.cmbcustomer & "*'"
End If
End If
If strCriteria <> "" Then
Me.Filter = strCriteria
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
DoCmd.GoToRecord , , acLast
Me.cmbcustomer = Me.cmbcustomer
End Function