Skip to content

Commit 865b547

Browse files
committed
split DBModif into DBModif and DBModifHelper
moved checkHiddenExcelInstance into Globals improved comments and docs createPivotTable now doesn't need a ExcelVersionForPivot setting, but determines the highest possible version itself. fixed collectSettings.vbs to get really all settings and sort correctly
1 parent 004e75e commit 865b547

20 files changed

+976
-934
lines changed

Distribution/DBaddin32.xll

0 Bytes
Binary file not shown.

Distribution/DBaddin64.xll

0 Bytes
Binary file not shown.

docs/DBFuncs.md

+7
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ This builds a Powerquery compliant string (quoted using double quotes) from the
257257

258258
When E1 contains "test", this results in "a test".
259259

260+
#### preventRefresh
261+
262+
<pre lang="vb">preventRefresh(setPreventRefresh, onlyForThisWB (optional))</pre>
263+
264+
sets preventRefresh flag globally or just for the current workbook (if onlyForThisWB is set), similar to clicking the ribbon toggle button "refresh prevention". This setting is not persisted with the workbook!
265+
266+
260267
### Modifications of DBFunc Behaviour
261268

262269
There are some options to modify

docs/DBaddin.ldproj

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0"?>
2+
<project xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<files>
4+
<file>../source/DBaddin.sln</file>
5+
</files>
6+
<RemovedAssemblies />
7+
<filters>
8+
<filter>Private</filter>
9+
<filter>Protected</filter>
10+
<filter>InternalProtected</filter>
11+
</filters>
12+
<configuration>Release</configuration>
13+
<language>VisualBasic</language>
14+
</project>
-1.33 KB
Binary file not shown.

source/AdHocSQL.vb

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
Imports System.Data
1+
Imports System.ComponentModel ' for BackgroundWorker callback handling
2+
Imports System.Data
23
Imports System.Windows.Forms
3-
Imports System.ComponentModel ' for BackgroundWorker callback handling
4+
45

56
''' <summary>User-form for ad-hoc SQL execution</summary>
67
Public Class AdHocSQL
78
''' <summary>common connection settings factored in helper class</summary>
89
Private myDBConnHelper As DBConnHelper
910
''' <summary>stored environment to reset after change</summary>
10-
Private storedUserSetEnv As String = ""
11+
Private ReadOnly storedUserSetEnv As String = ""
1112
''' <summary>stored database to reset after change</summary>
12-
Private userSetDB As String = ""
13+
Private ReadOnly userSetDB As String = ""
1314

1415
''' <summary>create new AdHocSQL dialog</summary>
1516
''' <param name="SQLString"></param>
@@ -322,6 +323,9 @@ Public Class AdHocSQL
322323
sender.CurrentRow.Cells(e.ColumnIndex).TooltipText = "Data raised exception: " + e.Exception.Message + " (" + e.Context.ToString() + ")"
323324
End Sub
324325

326+
''' <summary>show context menu for SQLText, displaying config menu as a MenuStrip</summary>
327+
''' <param name="sender"></param>
328+
''' <param name="e"></param>
325329
Private Sub SQLText_MouseDown(sender As Object, e As MouseEventArgs) Handles SQLText.MouseDown
326330
If e.Button = Windows.Forms.MouseButtons.Right Then
327331
Me.ConfigMenuStrip = ConfigFiles.ConfigContextMenu

source/AddInEvents.vb

+8-22
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,9 @@ Imports Microsoft.Office.Interop
44
Imports Microsoft.Office.Interop.Excel ' for event procedures...
55
Imports Microsoft.Office.Core
66
Imports Microsoft.Vbe.Interop
7-
Imports System.Diagnostics
87
Imports System.Runtime.InteropServices
98
Imports System.Collections.Generic
109

11-
Public Module CheckInstance
12-
Public Sub checkHiddenExcelInstance()
13-
Try
14-
' check for multiple excel instances
15-
If Process.GetProcessesByName("Excel").Length > 1 Then
16-
For Each procinstance As Process In Process.GetProcessesByName("Excel")
17-
If procinstance.MainWindowTitle = "" Then
18-
UserMsg("Another hidden excel instance detected (PID: " + procinstance.Id + "), this may cause problems with querying DB Data")
19-
End If
20-
Next
21-
End If
22-
Catch ex As Exception : End Try
23-
End Sub
24-
End Module
2510

2611
''' <summary>AddIn Connection class, also handling Events from Excel (Open, Close, Activate)</summary>
2712
<ComVisible(True)>
@@ -63,9 +48,9 @@ Public Class AddInEvents
6348
End If
6449
Catch ex As Exception : End Try
6550
' for finding out what happened attach internal trace to ExcelDNA LogDisplay
66-
theLogDisplaySource = New TraceSource("ExcelDna.Integration")
51+
theLogDisplaySource = New Diagnostics.TraceSource("ExcelDna.Integration")
6752
' and also define a LogSource for DBAddin itself for writing text log messages
68-
theLogFileSource = New TraceSource("DBAddin")
53+
theLogFileSource = New Diagnostics.TraceSource("DBAddin")
6954

7055
' IntelliSense needed for DB- and supporting functions
7156
ExcelDna.IntelliSense.IntelliSenseServer.Install()
@@ -292,7 +277,7 @@ done:
292277
''' <param name="Wb"></param>
293278
Private Sub Application_WorkbookActivate(Wb As Excel.Workbook) Handles Application.WorkbookActivate
294279
' avoid when being activated by DBFuncsAction
295-
If Not DBModifs.preventChangeWhileFetching And Not Wb.IsAddin Then
280+
If Not DBModifHelper.preventChangeWhileFetching And Not Wb.IsAddin Then
296281
' in case AutoOpen hasn't been triggered (e.g. when Excel was started via Internet Explorer)...
297282
If DBModifDefColl Is Nothing Then
298283
DBModifDefColl = New Dictionary(Of String, Dictionary(Of String, DBModif))
@@ -343,7 +328,7 @@ done:
343328
''' <param name="cbName">name of command button, defines whether a DBModification is invoked (starts with DBMapper/DBAction/DBSeqnce)</param>
344329
Private Shared Sub cbClick(cbName As String)
345330
' reset non interactive messages (used for VBA invocations) and hadError for interactive invocations
346-
nonInteractiveErrMsgs = "" : DBModifs.hadError = False
331+
nonInteractiveErrMsgs = "" : DBModifHelper.hadError = False
347332
Dim DBModifType As String = Left(cbName, 8)
348333
If DBModifType <> "DBSeqnce" Then
349334
Dim targetRange As Excel.Range
@@ -418,15 +403,15 @@ done:
418403
''' <param name="Sh"></param>
419404
Private Sub Application_SheetActivate(Sh As Object) Handles Application.SheetActivate
420405
' avoid when being activated by DBFuncsAction
421-
If Not DBModifs.preventChangeWhileFetching Then
406+
If Not DBModifHelper.preventChangeWhileFetching Then
422407
' only when needed assign button handler for this sheet ...
423408
If Not IsNothing(DBModifDefColl) AndAlso DBModifDefColl.Count > 0 Then assignHandler(Sh)
424409
End If
425410
End Sub
426411

427412
Private WbIsClosing As Boolean = False
428413

429-
''' <summary>Clean up after closing workbook, only set flag here, actual cleanup is only done if workbook is really closed (in WB_Deactivate event)</summary>
414+
''' <summary>Clean up after closing workbook, only set flag here, the actual cleanup is only done if workbook is really closed (in WB_Deactivate event)</summary>
430415
''' <param name="Wb"></param>
431416
''' <param name="Cancel"></param>
432417
Private Sub Application_WorkbookBeforeClose(Wb As Workbook, ByRef Cancel As Boolean) Handles Application.WorkbookBeforeClose
@@ -437,6 +422,7 @@ done:
437422
''' <param name="Wb"></param>
438423
Private Sub Application_WorkbookDeactivate(Wb As Workbook) Handles Application.WorkbookDeactivate
439424
Try
425+
If WbIsClosing AndAlso preventRefreshFlagColl.ContainsKey(Wb.Name) Then preventRefreshFlagColl.Remove(Wb.Name)
440426
If WbIsClosing AndAlso Not IsNothing(DBModifDefColl) AndAlso DBModifDefColl.Count > 0 Then
441427
DBModifDefColl.Clear()
442428
theRibbon.Invalidate()
@@ -452,7 +438,7 @@ done:
452438
''' <param name="Target"></param>
453439
Private Sub Application_SheetChange(Sh As Object, Target As Range) Handles Application.SheetChange
454440
' avoid entering into insert/update check resp. doCUDMarks if not list-object (data table), whole column modified, no DBMapper present and prevention while fetching (on refresh) being set
455-
If Not IsNothing(Target.ListObject) AndAlso Not Target.Rows.Count = Sh.Rows.Count AndAlso DBModifDefColl.ContainsKey("DBMapper") AndAlso Not DBModifs.preventChangeWhileFetching Then
441+
If Not IsNothing(Target.ListObject) AndAlso Not Target.Rows.Count = Sh.Rows.Count AndAlso DBModifDefColl.ContainsKey("DBMapper") AndAlso Not DBModifHelper.preventChangeWhileFetching Then
456442
Dim targetName As String = getDBModifNameFromRange(Target)
457443
If Left(targetName, 8) = "DBMapper" Then
458444
DirectCast(DBModifDefColl("DBMapper").Item(targetName), DBMapper).insertCUDMarks(Target)

source/ConfigFiles.vb

+20-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Public Module ConfigFiles
2424
Public specialFolderMaxDepth As Integer
2525
''' <summary>store found sub-menus in this collection</summary>
2626
Private specialConfigFoldersTempColl As Collection
27+
''' <summary>collection structure for the two menu types, one in ribbon (Xelement) and MenuStrip (ToolStripMenuItem, used in AdHocSQL/SQLText context menu)</summary>
2728
Private Structure MenuClassStruct
2829
Dim ribbonmenu As XElement
2930
Dim contextmenu As ToolStripMenuItem
@@ -147,8 +148,9 @@ Public Module ConfigFiles
147148

148149
''' <summary>creates the ribbon config tree menu and the context menu for the AdHocSQL Dialog by reading the menu elements from the config store folder files/sub-folders</summary>
149150
Public Sub createConfigTreeMenu()
150-
Dim currentBar, button As XElement
151-
Dim currentStrip As New ToolStripMenuItem
151+
' collecting menu items in
152+
Dim currentBar As XElement ' for ribbon
153+
Dim currentStrip As New ToolStripMenuItem ' for context menu in AdHocSQL/SQLText
152154

153155
' also get the documentation that was provided in setting ConfigDocQuery into ConfigDocCollection (used in config menu when clicking entry + Ctrl/Shift)
154156
Dim ConfigDocQuery As String = fetchSetting("ConfigDocQuery" + env(), fetchSetting("ConfigDocQuery", ""))
@@ -162,8 +164,8 @@ Public Module ConfigFiles
162164
Else
163165
' top level menu
164166
currentBar = New XElement(xnspace + "menu")
165-
' add refresh button to top level
166-
button = New XElement(xnspace + "button")
167+
' add refresh button to top level only for ribbon menu
168+
Dim button As New XElement(xnspace + "button")
167169
button.SetAttributeValue("id", "refreshConfig")
168170
button.SetAttributeValue("label", "refresh DBConfig Tree")
169171
button.SetAttributeValue("imageMso", "Refresh")
@@ -176,12 +178,13 @@ Public Module ConfigFiles
176178
specialConfigFoldersTempColl = Nothing
177179
ExcelDnaUtil.Application.StatusBar = ""
178180
currentBar.SetAttributeValue("xmlns", xnspace)
179-
' avoid exception in ribbon...
181+
' avoid exception in ribbon by respecting the entry limit...
180182
ConfigMenuXML = currentBar.ToString()
181183
If ConfigMenuXML.Length > maxSizeRibbonMenu Then
182184
UserMsg("Too many entries in " + ConfigStoreFolder + ", can't display them in a ribbon menu ..")
183185
ConfigMenuXML = "<menu xmlns='" + xnspace.ToString() + "'><button id='refreshDBConfig' label='refresh DBConfig Tree' imageMso='Refresh' onAction='refreshDBConfigTree'/></menu>"
184186
End If
187+
' add all collected ToolStripMenuItem of currentStrip to top-level menu, copying needed because AddRange removes items from original collection
185188
Dim addedContextMenu As ToolStripMenuItem()
186189
ReDim addedContextMenu(currentStrip.DropDownItems.Count - 1)
187190
currentStrip.DropDownItems.CopyTo(addedContextMenu, 0)
@@ -252,6 +255,7 @@ Public Module ConfigFiles
252255
' normal case or max menu depth branch: just follow the path and enter all entries as buttons
253256
Else
254257
For i = 0 To UBound(fileList)
258+
' add ribbon menu leaf element
255259
newBar = New XElement(xnspace + "button")
256260
menuID += 1
257261
newBar.SetAttributeValue("id", "m" + menuID.ToString())
@@ -260,6 +264,7 @@ Public Module ConfigFiles
260264
newBar.SetAttributeValue("label", Folderpath + Left$(fileList(i).Name, Len(fileList(i).Name) - 4))
261265
newBar.SetAttributeValue("onAction", "getConfig")
262266
currentBar.Add(newBar)
267+
' add context menu strip leaf element (including event handler)
263268
Dim eventHandler As New System.EventHandler(AddressOf contextMenuClickEventHandler)
264269
newStrip = New ToolStripMenuItem(text:=Folderpath + Left$(fileList(i).Name, Len(fileList(i).Name) - 4), image:=Nothing, onClick:=eventHandler) With {
265270
.Tag = rootPath + "\" + fileList(i).Name,
@@ -278,11 +283,13 @@ Public Module ConfigFiles
278283
ExcelDnaUtil.Application.StatusBar = "Filling DBConfigs Menu: " + rootPath + "\" + DirList(i).Name
279284
' only add new menu element if below max. menu depth for ribbons
280285
If MenuFolderDepth < maxMenuDepth Then
286+
' add ribbon menu element
281287
newBar = New XElement(xnspace + "menu")
282288
menuID += 1
283289
newBar.SetAttributeValue("id", "m" + menuID.ToString())
284290
newBar.SetAttributeValue("label", DirList(i).Name)
285291
currentBar.Add(newBar)
292+
' add context menu strip element (no event handler needed)
286293
newStrip = New ToolStripMenuItem With {
287294
.Text = DirList(i).Name
288295
}
@@ -292,6 +299,7 @@ Public Module ConfigFiles
292299
MenuFolderDepth -= 1
293300
Else
294301
newBar = currentBar
302+
newStrip = currentStrip
295303
readAllFiles(rootPath + "\" + DirList(i).Name, newBar, newStrip, Folderpath + DirList(i).Name + "\")
296304
End If
297305
Next
@@ -300,7 +308,7 @@ Public Module ConfigFiles
300308
End Try
301309
End Sub
302310

303-
''' <summary>the event-handler for the context menu entries of the AdHocSQL context menu</summary>
311+
''' <summary>the event-handler for the context menu entries of the AdHocSQL context menu, used either to show the documentation for the entries or to insert the queries defined in the xcl files</summary>
304312
''' <param name="sender">the tool-strip menu item that sent the event</param>
305313
''' <param name="e"></param>
306314
Public Sub contextMenuClickEventHandler(sender As Object, e As Object)
@@ -310,12 +318,12 @@ Public Module ConfigFiles
310318
' get the file content defined in sender.Tag (absolute path of xcl definition file)
311319
' ConfigArray: Configs are tab separated pairs of <RC location vbTab function formula> vbTab <...> vbTab...
312320
Dim ConfigArray As String() = Split(getFileContent(sender.Tag), vbTab)
313-
' fetch query out of DBListFetch definition
321+
' fetch query from DBListFetch definition (includes quotes at beginning/end)
314322
Dim functionParts As String() = functionSplit(ConfigArray(1), ",", """", "DBListFetch", "(", ")")
315323
If functionParts IsNot Nothing Then
316324
' either put query into SQLText content if empty
317325
If theAdHocSQLDlg.SQLText.Text.Length = 0 Then
318-
theAdHocSQLDlg.SQLText.Text = functionParts(0).Substring(1, functionParts(0).Length - 2)
326+
theAdHocSQLDlg.SQLText.Text = functionParts(0).Substring(1, functionParts(0).Length - 2) ' remove quotes at beginning/end
319327
Else
320328
' or attach extracted table (after FROM part) to existing content, if a FROM part is available
321329
Dim startPosOfTable As Integer = functionParts(0).IndexOf("FROM")
@@ -359,6 +367,7 @@ Public Module ConfigFiles
359367
' end node: add callable entry (= button)
360368
If InStr(1, nameParts, " ") = 0 Or MenuDepth >= specialFolderMaxDepth Or MenuDepth + MenuFolderDepth >= maxMenuDepth Then
361369
Dim entryName As String = Mid$(fullPathName, InStrRev(fullPathName, "\") + 1)
370+
' add ribbon menu leaf element
362371
newBar = New XElement(xnspace + "button")
363372
menuID += 1
364373
newBar.SetAttributeValue("id", "m" + menuID.ToString())
@@ -367,6 +376,7 @@ Public Module ConfigFiles
367376
newBar.SetAttributeValue("tag", fullPathName)
368377
newBar.SetAttributeValue("onAction", "getConfig")
369378
currentBar.Add(newBar)
379+
' add context menu strip leaf element (including event handler)
370380
Dim eventHandler As New System.EventHandler(AddressOf contextMenuClickEventHandler)
371381
newStrip = New ToolStripMenuItem(text:=Left$(entryName, Len(entryName) - 4), image:=Nothing, onClick:=eventHandler) With {
372382
.Tag = fullPathName,
@@ -385,11 +395,13 @@ Public Module ConfigFiles
385395
newMenuClass.ribbonmenu = newBar
386396
newMenuClass.contextmenu = newStrip
387397
Else
398+
' add ribbon menu element
388399
newBar = New XElement(xnspace + "menu")
389400
menuID += 1
390401
newBar.SetAttributeValue("id", "m" + menuID.ToString())
391402
newBar.SetAttributeValue("label", newName)
392403
currentBar.Add(newBar)
404+
' add context menu strip element (no event handler needed)
393405
newStrip = New ToolStripMenuItem With {
394406
.Text = newName
395407
}

source/DBDocumentation.vb

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Imports System.Windows.Forms
22

3+
''' <summary>Simple Popup Window for displaying Database documentation</summary>
34
Public Class DBDocumentation
45
Private Sub CancelBtn_Click(sender As Object, e As EventArgs) Handles CancelBtn.Click
56
Me.Close()

0 commit comments

Comments
 (0)