I'm using ADP in access to sql server 2005, trying to get a button click event working. I think i'm missing a reference in the VB editor but i don't know which one. I'm getting this on any of my onclick button events: The Expression OnClick you entered as the event property setting produced the following error: Invalid outside procedure
Here's my code, it happens on btnBrowse_Click event which calls GetCSVName
'column headers in csv file
Const hdrIntMonthID = "intMonthID"
Const hdrIntBranch = "intBranch"
Const hdrIntReportMonthID = "intReportMonthID"
Const hdrTxtMachCode = "txtMachCode"
Const hdrIntDistTerritoryID = "intDistTerritoryID"
Const hdrIntUSUnits = "intUSUnits"
Const hdrTxtMachCategory = "txtMachCategory"
Const hdrDttLastUpd = "dttLastUpd"
Dim hdrLst() As String
Dim strTableName As String
strTableName = "tblBranchDataEntry"
Dim rstData As Recordset
Set rstData = New ADODB.Recordset
Dim strSQL As String
Sub GetCSVName()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(mso
FileDialog
Open)
With dlgOpen
.AllowMultiSelect = False
.Filters.Clear
.Filters.Add "Comma Delimited Files", "*.csv"
.Show
If .SelectedItems.Count > 0 Then
txtCSVName.SetFocus
txtCSVName.Text = .SelectedItems.Item(1)
End If
End With
End Sub
Sub LoadCSV(fn As String)
'we don't want to delete records, we want to append to this table
FN1 = FreeFile
Open fn For Input As FN1 'open file
Line Input #FN1, t 'the first line of text is headers
ParseCommas t, hdrLst 'break down commas in header
RowNumber = 0
'repeat until we get to the end of the file.
Do While Not EOF(FN1)
Line Input #FN1, t
'If Len(t) > 0 Then
RowNumber = RowNumber + 1
ParseCommas t, RowData
' For k = LBound(hdrLst) To UBound(hdrLst)
' Select Case UCase(hdrLst(k))
' Case hdrIntMonthID
'End If
SaveDataRow RowData
Loop
Close #FN1
Set rs = Nothing
End Sub
Sub ParseCommas(h As String, a() As String)
Dim cp As Integer 'comma position
Dim ip As Integer 'index position
Dim ht As String, ht2 As String 'header text
ip = 1
ReDim Preserve a(0)
Do While ip < Len(h)
cp = InStr(ip, h, ",") 'find next comma
If cp > 0 Then 'if we find a comma
ht = Trim(Mid(h, ip, cp - ip)) 'get text between prev & next commas
If Left(ht, 1) = """" Then 'is it a quote mark?
Do Until Right(ht, 1) = """" 'find the other one
cp = InStr(cp + 1, h, ",") 'find next comma
ht = Mid(h, ip, cp - ip) 'get the rest of it
Loop
ht = Mid(ht, 2, Len(ht) - 2) 'get everything but quote marks
End If
ht = TrimTabs(Trim(ht)) 'strip spaces then tabs
a(UBound(a)) = ht 'add to list
ReDim Preserve a(UBound(a) + 1)
Else
GoTo EarlyExit 'ran out of commas
End If
ip = cp + 1 'move past comma
Loop
EarlyExit:
'out of commas, copy remainder of string
If ip < Len(h) Then a(UBound(a)) = Right(h, Len(h) - ip + 1)
End Sub
Sub SaveDataRow(d() As String)
strSQL = "INSERT INTO " & strTableName & _
" (intMonthID,intBranch,intR
eportMonth
ID,txtUSMT
CMachCode,
intDistTer
ritoryID,i
ntUSUnits,
txtMachCat
egory,dttL
astUpd)" & _
" VALUES(" & d(0) & "," & d(1) & "," & d(2) & "," & d(3) & "," & d(4) & "," & d(5) & "," & d(6) & "," & Now & ")"
rstData.Open strSQL, CurrentProject.Connection,
adOpenKeyset, adLockOptimistic
End Sub
Private Sub btnBrowse_Click()
GetCSVName
End Sub
Start Free Trial