1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
| Option Compare Database
Option Explicit
Dim db As Database
Function ChangeTmpView(strTmpView As String, Optional strView As String)
Dim View, tmpView As QueryDef
Set db = CurrentDb
Set tmpView = db.QueryDefs(strTmpView)
If strView = "" Then
tmpView.SQL = "SELECT """" AS Ïîëå"
Else
Set View = db.QueryDefs(strView)
tmpView.SQL = View.SQL
End If
Set View = Nothing
Set tmpView = Nothing
Set db = Nothing
End Function
Function ReplaceSymbols(str As String) As String
Dim s, strNew As String
Do While Len(str) > 0
s = Left(str, 1)
str = Right(str, Len(str) - 1)
If s Like "[À-ÿ]" Or s Like "[A-z]" Or s = "_" Or s = "-" Then
strNew = strNew + s
Else
If Right(strNew, 1) <> " " Then strNew = strNew & " "
End If
Loop
ReplaceSymbols = strNew
End Function
Private Sub Form_Close()
Call ChangeTmpView("fsub_Views")
End Sub
Private Sub Form_Load()
Me.fld_View = ""
Me.fld_FilePath = ""
Me.fld_FolderPath = ""
Me.fld_FileName = ""
Me.fld_ReportTitle = ""
Me.fsub_Views.Requery
swh_ExistingFile = False
swh_NewFile = False
Call ChangeTmpView("fsub_Views")
End Sub
Private Sub fld_View_Change()
On Error Resume Next
Call ChangeTmpView("fsub_Views", Me.fld_View)
Me![fsub_Views].SourceObject = ""
Me![fsub_Views].SourceObject = "Çàïðîñ.fsub_Views"
Me.fld_ReportTitle = ""
Me.lst_ViewFieldsX.Requery
Me.lst_ViewFieldsY.Requery
End Sub
Private Sub bt_SelectFile_Click()
Dim FileName As String
FileName = getFile("Âûáîð ôàéëà", "C:\Users\" & Environ("USERNAME") & "\Desktop\", "Ôàéëû Excel", "*.xls*")
If FileName = "" Then Exit Sub
Me.fld_FilePath = FileName
End Sub
Private Sub bt_SelectFilePath_Click()
Dim FolderName As String
FolderName = getFolder("Âûáîð ïàïêè îò÷åòà")
If FolderName = "" Then Exit Sub
Me.fld_FolderPath = FolderName
End Sub
Private Sub swh_ExistingFile_BeforeUpdate(Cancel As Integer)
If Not swh_ExistingFile Then Cancel = True
End Sub
Private Sub swh_ExistingFile_AfterUpdate()
swh_NewFile = False
Me.fld_FilePath.Visible = True
Me.bt_SelectFile.Visible = True
Me.fld_ReportTitle.Visible = True
Me.fld_FolderPath.Visible = False
Me.bt_SelectFilePath.Visible = False
Me.fld_FileName.Visible = False
End Sub
Private Sub swh_NewFile_BeforeUpdate(Cancel As Integer)
If Not swh_NewFile Then Cancel = True
End Sub
Private Sub swh_NewFile_AfterUpdate()
swh_ExistingFile = False
Me.fld_FolderPath.Visible = True
Me.bt_SelectFilePath.Visible = True
Me.fld_FileName.Visible = True
Me.fld_ReportTitle.Visible = True
Me.fld_FilePath.Visible = False
Me.bt_SelectFile.Visible = False
End Sub
Private Sub bt_CreateChart_Click()
Dim VExcel As Object
Dim objOpen As Object
Dim wbTo As Workbook
Dim wb As Workbook
Dim wsTo As Worksheet
Dim FileName As String
Dim fOpen As String
Dim ws, s As String
Dim i, j As Long
Dim flds As Variant
On Error Resume Next
' îïðåäåëåíèå èìåíè ôàéëà è îòêðûòèå ðàáî÷åé êíèãè
If Me.swh_NewFile = True Then
' ñîçäàíèå íîâîãî ôàéëà
If Me.fld_FolderPath = "" Then MsgBox "Íå çàäàíî ðàñïîëîæåíèå ôàéëà": Exit Sub
If Me.fld_FileName = "" Then MsgBox "Íå çàäàíî èìÿ ôàéëà": Exit Sub
Set VExcel = CreateObject("Excel.Application")
VExcel.SheetsInNewWorkbook = 1
Set wbTo = VExcel.Workbooks.Add
wbTo.SaveAs FileName:=Me.fld_FolderPath & "\" & Me.fld_FileName, FileFormat:=xlExcel8
Else
' îòêðûòèå ñóùåñòâóþùåãî ôàéëà
' ïðîâåðÿåì, îòêðûò ëè ôàéë èëè ýêçåìïëÿð Excel
If Me.swh_ExistingFile = False Then MsgBox "Ôàéë íå âûáðàí": Exit Sub
FileName = Me.fld_FilePath
If CheckFileIsLocked(FileName) = True Then
Set VExcel = GetObject(, "Excel.Application")
For Each wb In VExcel.Workbooks
fOpen = wb.Path & "\" & wb.Name
If fOpen = FileName Then Set wbTo = wb: Exit For
Next wb
If wbTo Is Nothing Then Exit Sub
Set wbTo = VExcel.Workbooks(FileName)
Else
If IsError(GetObject(, "Excel.Application")) Then
Set VExcel = CreateObject("Excel.Application")
Else
Set VExcel = GetObject(, "Excel.Application")
End If
Set wbTo = VExcel.Workbooks.Open(FileName)
End If
End If
' åñëè wbTo íå óäàëîñü îïðåäåëèòü, âûõîäèì èç ïðîãðàììû
If wbTo Is Nothing Then MsgBox "Íå óäàëîñü ñîçäàòü èëè îïðåäåëèòü ðàáî÷óþ êíèãó." & vbNewLine & "Âûïîëíåíèå ïðîãðàììû îñòàíîâëåíî": Exit Sub
' ñîçäàåì íîâûé ëèñò äëÿ äèàãðàììû
VExcel.Visible = True
'Set wbTo = VExcel.Workbooks.Open(FileName)
Set wsTo = wbTo.Worksheets.Add
' åñëè ëèñò ñ òàêèì íàçâàíèåì óæå åñòü, ñîçäàåì íîâûé ëèñò ñ íîìåðîì +1
If CheckWorksheetExist(ReplaceSymbols(Me.fld_View), wbTo) = True Then
ws = ReplaceSymbols(Me.fld_View)
Do While CheckWorksheetExist(CStr(ws), wbTo) = True
s = Right(ws, 1)
If s Like "#" Then
ws = Left(ws, Len(ws) - 1) & CInt(s) + 1
Else
ws = ws & "1"
End If
Loop
wsTo.Name = ws
Else
wsTo.Name = ReplaceSymbols(Me.fld_View)
End If
' çàïèñûâàåì äàííûå èç access è ñòðîèì äèàãðàììó
' ñîçäàåì âðåìåííóþ òàáëèöó è êîïèðóåì â íåå ïîëÿ Me.fsub_Views.Form.Recordset
' îïðåäåëÿåì ïåðå÷åíü âûáðàííûõ ïîëåé è äîáàâëÿåì èõ âî âðåìåííóþ òàáëèöó
flds = Array(Me.lst_ViewFieldsX.Value)
For i = 0 To Me.lst_ViewFieldsY.ListCount - 1
If Me.lst_ViewFieldsY.Selected(i) Then
ReDim Preserve flds(UBound(flds) + 1)
flds(UBound(flds)) = Me.lst_ViewFieldsY.ItemData(i)
End If
Next
' ïåðåíîñèì äàííûå âî âðåìåííóþ òàáëèöó
' êîïèðîâàíèå äàííûõ äëÿ äèàãðàììû
With Me.fsub_Views.Form.Recordset
' øàïêà äëÿ èñõîäíûõ äàííûõ äèàãðàììû
For i = 1 To .Fields.Count + 1
wsTo.Cells(1, i) = .Fields(i - 1).Name
With wsTo.Cells(1, i).Font
.Bold = True
.Italic = False
.Size = 12
.Name = "Times New Roman"
End With
Next
.MoveFirst
End With
' ïåðåíîñ äàííûõ â Excel
With wsTo
.Range("A2").CopyFromRecordset Me.fsub_Views.Form.Recordset: Me.fsub_Views.Form.Recordset.MoveFirst
' àâòîìàòè÷åñêèé ïîäáîð øèðèíû ñòîëáöîâ
.UsedRange.CurrentRegion.EntireColumn.AutoFit
' çàãîëîâîê äèàãðàììû
.Cells(1, i + 2).Value = Me.fld_ReportTitle
With wsTo.Cells(1, i + 2).Font
.Bold = True
.Italic = True
.Size = 14
.Name = "Times New Roman"
End With
' óäàëåíèå íåíóæíûõ ñòîëáöîâ è ïåðåñòàíîâêà ñòîëáöà x â íà÷àëî
j = 1
For i = 0 To Me.fsub_Views.Form.Recordset.Fields.Count
If CheckValueInArray(flds, .Cells(1, j)) = True Then
If .Cells(1, j).Value = flds(0) Then
VExcel.Application.CutCopyMode = False
.Columns(j).Select
Selection.Cut
Range("A1").Select
Selection.Insert Shift:=xlToRight
End If
Else
.Columns(j).Delete
j = j - 1
End If
j = j + 1
Next
' ïîñòðîåíèå äèàãðàììû
.Shapes.AddChart2(, xlLineMarkers, .Cells(1, 1).Width * (j + 1), .Cells(1, j + 2).Height * 2, 700, 300).Name = Me.fld_View
.Shapes(Me.fld_View).SetSourceData Source:=.Columns("$A:$" & ColumnChar(j - 1)), PlotBy:=xlColumns
'.Shapes(Me.fld_View).ChartTitle.Text = Me.fld_View
.Cells(1, 1).Select
End With
wbTo.Save
'wbTo.Close
'VExcel.Quit
Erase flds
Set wbTo = Nothing
Set VExcel = Nothing
End Sub
Function ColumnChar(ByVal col As Long) As String
On Error Resume Next
ColumnChar = Excel.Application.ConvertFormula("rc" & col, xlR1C1, xlA1)
ColumnChar = Replace(Mid(ColumnChar, 2), 1, "")
End Function
Function CheckValueInArray(arr As Variant, val As Variant) As Boolean
Dim i As Variant
CheckValueInArray = False
For Each i In arr
If i = val Then CheckValueInArray = True: Exit For
Next i
End Function |