Microsoft Forms error after update.

Copper Contributor

This is a system we use that pulls information from a data system. The compile error I get is "Method or data member not found." This started happening after a system update. I don't know much about the system or Visual Basic but it seems like it's not happy with the syntax in the UserForm_Initialize sub. It highlights the Me.DTPStartTime lines. 

 

 

 

Private mCN As ADODB.Connection
Private mExcel As Excel.Application
Private mWBs As Excel.Workbooks
Private mWB As Excel.Workbook

Private Sub cmdGetData_Click()
Dim startdate As String
Dim enddate As String
Dim retval As Long

startdate = Format(Me.DTPStartTime.Value, "mm/dd/yyyy") & " 00:00:00"
enddate = Format(Me.DTPEndTime.Value, "mm/dd/yyyy") & " 00:00:00"
retval = ExportDataToExcel(startdate, enddate)
End Sub

Private Sub cmdRefresh_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)

End Sub

Private Sub cmdRefresh_Click()
Dim startdate As String
Dim enddate As String
Dim retval As Long

startdate = Format(Me.DTPStartTime.Value, "mm/dd/yyyy") & " 00:00:00"
enddate = Format(Me.DTPEndTime.Value, "mm/dd/yyyy") & " 00:00:00"

retval = GetUniqueData("Part_Number", Me.cboParts, startdate, enddate)
retval = GetUniqueData("Line_Number", Me.cboLines, startdate, enddate)
retval = GetUniqueData("Reject_Code_Number", Me.cboRejects, startdate, enddate)
End Sub

Private Sub cmdTest1_Click()
Dim retval As Long

retval = CreateNewExcelWorkbook(Me.txtFileName.Text)
End Sub

Private Sub DTPEndTime_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)

End Sub

Private Sub DTPEndTime_CloseUp()

End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim answer As Integer

answer = MsgBox("Do you want to shutdown excel?", vbYesNo)
If answer = vbYes Then
Application.Quit
End If
End Sub


Private Sub UserForm_Initialize()
Set mCN = New ADODB.Connection
' mCN.Open "Provider=SQLOLEDB.1;Password=Password08;Persist Security Info=True;User ID=sa;Initial Catalog=Pokeyoke;Data Source=abourque-lt2\sql2008_full"
mCN.Open "Provider=SQLOLEDB.1;Password=Pokeyoke;Persist Security Info=True;User ID=Pokeyoke;Initial Catalog=Pokeyoke;Data Source=10.150.188.75"
Me.DTPStartTime = DateAdd("d", -1, Now)
Me.DTPEndTime = Now
'Me.DTPStartTime = "09/02/2014"
'Me.DTPEndTime = "09/04/2014"

End Sub

Private Function GetUniqueData(fieldname As String, cbo As ComboBox, startdate As String, enddate As String) As Long
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim wb As Excel.Workbook


On Error GoTo errorhandler
cbo.Clear

cbo.AddItem "All"
If startdate < enddate Then
'build sql to get unique parts
strsql = "Select distinct " & fieldname & " from ProductDataView where timestamp between '" & startdate & "' and '" & enddate & "' order by " & fieldname
rs.Open strsql, mCN, 3, 1

Do While Not rs.EOF
cbo.AddItem rs.Fields(0)
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
End If
cbo.ListIndex = 0
Exit Function

errorhandler:
GetUniqueParts = Err.Number

End Function

Private Function ExportDataToExcel(startdate As String, enddate As String) As Long
Dim rs As New ADODB.Recordset
Dim strsql As String
Dim whereClause As String
Dim retval As Long

On Error GoTo errorhandler

strsql = "Select * from ProductDataView where timestamp between '" & startdate & "' and '" & enddate & "'"
whereClause = ""

'now determine the whereclause
If Me.cboLines.Text <> "" And UCase(Me.cboLines.Text) <> "ALL" Then
whereClause = whereClause & " and Line_Number = " & Me.cboLines.Text
End If

'now determine the whereclause
If Me.cboParts.Text <> "" And UCase(Me.cboParts.Text) <> "ALL" Then
whereClause = whereClause & " and Part_Number = " & Me.cboParts.Text
End If

'now determine the whereclause
If Me.cboRejects.Text <> "" And UCase(Me.cboRejects.Text) <> "ALL" Then
whereClause = whereClause & " and Reject_Code_Number = " & Me.cboRejects.Text
End If

strsql = strsql & whereClause & " order by timestamp"
Debug.Print strsql
'rs.Open strSQL, mCN

retval = CreateNewExcelWorkbook(Me.txtFileName.Text)
retval = FillExcelWithData(mWB, strsql, mCN.ConnectionString)

Exit Function
errorhandler:

ExportDataToExcel = Err.Number
End Function
Private Function FillExcelWithData(wb As Workbook, strsql As String, connectstring As String) As Long
Dim Sheet As Worksheet
Dim i As Integer


mExcel.WindowState = xlMaximized
mWB.Activate
mWB.Sheets("Sheet1").Activate
Set Sheet = mWB.Sheets("Sheet1")
Sheet.Cells.ClearContents


Dim rs As New ADODB.Recordset
rs.Open strsql, mCN
For i = 0 To rs.Fields.Count - 1
Set cl = Sheet.Cells(1, i + 1)
cl.Value = rs.Fields(i).Name
Next
If Not rs.EOF Then
Sheet.Range("A2").CopyFromRecordset rs
End If
rs.Close

mWB.Activate

'now format the time column F
Sheet.Range("F2").EntireColumn.NumberFormat = "mm/dd/yyyy hh:mm:ss"
' ActiveCell.EntireColumn.Select
' Selection.NumberFormat = "mm/dd/yyyy hh:mm:ss"
Sheet.Columns.EntireColumn.AutoFit
'.Refresh
' Set mWB = mWBs.OpenDatabase(connectstring, strsql)
End Function
Private Function CreateNewExcelWorkbook(workbookName As String) As Long
On Error GoTo errorhandler

If mExcel Is Nothing Then
Set mExcel = New Excel.Application
End If

mExcel.Visible = True
Set mWB = mExcel.Workbooks.Add()
'oWB.SaveAs workbookName
Set mWBs = mExcel.Workbooks
Exit Function

'oWB.Name = workbookName
errorhandler:
CreateNewExcelWorkbook = Err.Number

End Function

 

Let me know if you need more information, thank you.

7 Replies

@IvanGVibra 

 

It appears the problem is that it cannot find "DTPStartTime" (and I think you will have the same problem for "DTPEndTime"). Normally, these would be either property procedures or public module level variables within the form's code module and would be initialized/set by the procedure that creates the form (or maybe in a class initialization procedure that was deleted). In this case, it appears they reference a worksheet range that contains the values. 

 

I'm guessing they've been deleted. If you have an older version of your file available, I would look at it to see what it was doing for the start and end times.

 

Edit: I attached an example of a userform using public property procedures to set/get a range variable and one that uses public variables. Look at the code in Sheet1 module to see how it is setting those properties before showing the form and then in the userform modules to see how it is storing the ranges that passed to the property procedures. But, these are a simple example, I don't know if your original file was doing anything to validate the information or use it for something else.

No luck with different files. I even commented those lines out and uncommented where they are set equal to specific dates and that didn't work.

Sorry, even though you mentioned the error was in the initialize procedure, I overlooked that procedure. It's more likely that DTPStartTime and DTPEndTime refer to controls on the userform itself. When looking at the userform, open the properties window (view/properties window) and then click on the start/end time controls and see if the name has been changed (assuming those controls exist at all).
The userform does not pop up anymore when the excel file is opened. I just get an error stating "Could not load some objects because they are not available on this machine." Does this indicate an issue with my computer or the computer the database is on?
Are you missing any reference libraries (in vba editor, click tools/references and see if any are marked "Missing").

You might also check to see if the solver add-in is loaded - I've read it can sometimes cause that error (in excel, click file/options/add-ins/go and uncheck solver if it is loaded). If it's not that, I would try unloading other add-ins one at a time to see if they are the problem.

Failing that, I would try repairing office through the control panel (programs and features, right click and select repair).

No references marked as missing.

Solver Add-in is listed. When I got to manage Add-ins I tried selecting and unselecting and still no difference.

I used a remote connection to run this excel file on the PC that the database is on and it seemed to work so the issue is definitely on my pc.
One other thing you could try is deleting the *.exd files from your computer, and force excel to rebuild them. These are control information cache files - once in a while when the software is updated, the cache file becomes outdated but MS does not automatically delete them in the update process.

https://www.greytrix.com/blogs/sageaccpacerp/2013/01/09/vba-macro-errors-related-to-exd-files/