Import Data from 3 MDBs and 1 CSV, use in DataGrid object - database

This is eventually to be run on a primitive Windows environment for a PLC, so I'm using Visual Basic .NET 4.0 in Studio 2013.
Anyways here's the challenge I expect to be fairly simple once get going: I've got 3 MDBs with 1 table each and 1 CSV file, which I need to import data from and provide various viewing/editing functionality.
These MDBs are essentially a list of parameters for various "program settings" run by the PLC, called "recipes". There are over 200 parameters, but for some reason they were limited to 100 per file, so they had to create 3 MDBs. But they're essentially representing one long list of parameters. So for instance, the PLC might execute 4 different formats, or recipes, each with a unique "IndexName" key. So in the first MDB, the table has 4 rows and 106 columns, and in the second one 4 rows and 110 columns.
Here's a screenshot of the table in Access
"Export Demo.zip" on this post
http://www.vbforums.com/showthread.php?719287-RESOLVED-mdb-database-to-csv-file
seemed rather promising, but it's in VB6 and it's a bit beyond me to adapt to my environment.
I can create one DataSource/DataSet per source MDB/CSV, but my objective is to populate a single DataGrid object with Data from all those sources. I'm currently looking into how I can consolidate the data into one DataTable.

Pulling a few bits and pieces from various sources together, I managed to get this working. Particularly useful was the DataTable.Merge function, took me a while to find that.
Not sure how useful this code might be for others, or if it goes against stackoverflow etiquette. I'm happy to fix accordingly, but I did want to share that I managed.
Obviously this isn't the complete code, but hopefully the most relevant bits. I'm happy to provide some more pieces if required. Code comments/improvements welcome, I'm just a beginner!
Sub LoadDataIntoDataGrid()
'Loads CSV Table, Transpose & Load 3 MDB tables, Capture each Table Params, & Merge all 3 into 1
'Adjusts the merged table according to the csv file
Dim CSVTable As DataTable, MDB0Table As DataTable, MDB1Table As DataTable, MDB2Table As DataTable, _
CRLTable As DataTable
Dim CSVRow As DataRow
Dim removeRows As New List(Of String)
'Populate CSV Table
CSVTable = ReadFromCSV("CSVFile.csv")
'Populate 3 MDB tables with transposed Data: Rows are parameters
MDB0Table = ReadFromMDB("First.mdb", "SELECT * from Table1")
MDB1Table = ReadFromMDB("Second.mdb", "SELECT * from Table2")
MDB2Table = ReadFromMDB("Third.mdb", "SELECT * from Table3")
'Put each Parameter name into MDBTableParams
For Each ParamName In MDB0Table.Rows
CRLForm.MDBTableParams(0).Add(ParamName(0))
Next
For Each ParamName In MDB1Table.Rows
CRLForm.MDBTableParams(1).Add(ParamName(0))
Next
For Each ParamName In MDB2Table.Rows
CRLForm.MDBTableParams(2).Add(ParamName(0))
Next
'Set Primary Keys
MDB0Table.PrimaryKey = New DataColumn() {MDB0Table.Columns("NamePar")}
MDB1Table.PrimaryKey = New DataColumn() {MDB1Table.Columns("NamePar")}
MDB2Table.PrimaryKey = New DataColumn() {MDB2Table.Columns("NamePar")}
CSVTable.PrimaryKey = New DataColumn() {CSVTable.Columns("NamePar")}
'Merge all 3 MDB Tables into 1
CRLTable = MDB0Table
CRLTable.Merge(MDB1Table)
CRLTable.Merge(MDB2Table)
'For each row in the merged table, either mark for removal or add the description from the CSV file
CRLTable.Columns.Add("Desc", GetType(String))
CRLTable.Columns("Desc").SetOrdinal(1) 'Make Description second column (after "NamePar")
For Each CRLRow In CRLTable.Rows
CSVRow = CSVTable.Rows.Find(CRLRow("NamePar"))
If CSVRow Is Nothing Then
removeRows.Add(CRLRow("NamePar")) 'tag rows to be removed due to missing in CSV
Else
CRLRow("Desc") = CSVRow("DESC_" & CRLForm.ComboBoxLanguage.Text)
End If
Next
'Remove Rows tagged for removal due to absence from CSV file
For Each row In removeRows
CRLTable.Rows.Remove(CRLTable.Rows.Find(row))
Next
'Set DataGridView data source as the cleaned merged table
CRLForm.DataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect ' Otherwise DataSource = CRLTable refuses to populate
CRLForm.DataGridView1.DataSource = CRLTable
CRLForm.DataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect
End Sub

Related

Populate a Multi-column Combobox with a 2D array on Access

I tried to follow this method:
ComboBox1.ColumnCount = 2
Dim Films(1 To 5, 1 To 2) As String
Dim i As Integer, j As Integer
Films(1, 1) = "Lord of the Rings"
Films(2, 1) = "Speed"
Films(3, 1) = "Star Wars"
Films(4, 1) = "The Godfather"
Films(5, 1) = "Pulp Fiction"
Films(1, 2) = "Adventure"
Films(2, 2) = "Action"
Films(3, 2) = "Sci-Fi"
Films(4, 2) = "Crime"
Films(5, 2) = "Drama"
ComboBox1.List = Films
source
But the .List property does not work on Access. Any ideas ?
As June7 said, use the ComboBox.AddItem() method in a loop. For your purposes, the ComboBox must not be bound to a data source: It's Row Source Type property should be set to "Value List". To add a multi-column string to a ComboBox row, use a semicolon to delimit the the columns. For example:
ComboBox1.AddItem (Films(1, 1) & ";" & Films(1, 2))
or
Dim rowStr As String
rowStr = Films(1, 1) & ";" & Films(1, 2)
ComboBox1.AddItem (rowStr)
AddItem() automatically appends the row to the end of the ComboBox's list, if you do not specify a row index parameter. For more info, see ComboBox.AddItem method at Office Dev Center.
Screenshot: VBA Demonstration Image
A "Form" in Access is not the same kind of element/object as a "UserForm" is in Excel where your "source" link points to (https://www.excel-easy.com/vba/examples/multicolumn-combo-box.html).
In Access it would be a good idea to get the information into your Combo Box (or List Box) from either a table or a query. You can of course code it with VBA, but then you might find yourself adding/editing a hole lot of VBA here and there, as in Access it all goes more naturally by using SQL and the database engine.
This is a larger topic, but basically you should probably have different tables for "Films" and for "Categories"
Table1:
Table2:
Then you should define the relationships since most likely there are different amount of films in your database than there are categories. Saying that we would like to avoid a situation that you would have to add another movie, let's say "Die hard" into your movie list. That would probably fall into the category "Action". In the database we do not want to repeat ourselves. Just we will, by ID, refer to categoryID by it's value.
So, having done that you need to create a form in Access. Create maybe a query that will get the values for you:
After this you can define the source for the combo e.g. by using wizard:
So this way you can maintain each of the lists separately in their own tables.
Here is the query that got created:
On the Data tab you can decide which bound column to use relative to datasource.
On the Format tab you can adjust the widths of the columns in your combobox. Use 0 length to hide a column.
This way no VBA code is needed.
If needed it is also possible to create or edit the queries with VBA but that is another story.
Hope this helps.

3D Datastructure with Index

This is my first question on stackoverflow, and I am earnestly open to feedback on how/where/when to ask better questions and how to contribute to stackoverflow better.
Background:
My ultimate goal is to graph projected equipment usage by date at various test labs.
I have identical equipment in use at several labs, and I'm creating a sheet that will show me a future projection of equipment usage at each lab.
What I'm Starting With:
I have an Excel document with several worksheets, each containing information on what equipment will be used at which test house during what period of time.
My Goal:
To create a graph of equipment usage for each test lab. The graph will show how many of each piece of equipment are in use for a given date. My intention is to have a chart series for each type of equipment with Date as the X-axis and the number of pieces of that equipment in use on the Y-axis.
What I've Done So Far:
I have written code that loops through all my information sheets and creates a vba collection of every unique test lab name and a separate vba collection of every unique piece of equipment I want to track. This code also finds the first date and last date any piece of equipment is used.
Help Request:
Because I essentially have three "dimensions" - Test Lab, Piece of Equipment, and Equipment Use Date - I had planned to use a 3D array to aggregate all my data and provide the source for my usage graphs. This array would have equipment as one dimension, date as the second, and test lab as the third.
However, as I've considered this implementation, it seems rather clumsy. It will hold all my data, but, as far as I can see, I can't refer to the elements of the array by keys or labels. I would have to create separate 2D arrays to hold index labels for each dimension of the 3D array.
Is there a 3D data structure in Excel VBA that supports index keys for each dimension?
Failed Searches and Attempts:
I first tried to create a unique array to hold equipment and usage date, each array named for a unique test lab. I learned from this post that I am not able to dynamically create and name an undefined number of new arrays within a sub: Naming an array using a variable.
I then looked into whether I could use the collections I had already created to somehow function as labels for the array indices, but it seems that I'm not able to find the collection index by the key. I would have to loop through the collection to find the index every time I want to reference an element in the 3D array: Retrieve the index of an object stored in a collection using its key (VBA).
If you need to call out a collection by key, that collection should instead be declared as a dictionary.
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
dict(Key) = Value
It is much more powerful than a collection. I hope that helps.
FULL INFORMATION: https://excelmacromastery.com/vba-dictionary/
I implemented information from all the comments and answers I received. Thank you Jeremy, Victor K, and HackSlash!
Here's the solution that worked for me in a nutshell: An array of a user-defined data type containing arrays of a user-defined data type containing scripting dictionaries, i.e. an array of arrays of dictionaries. I also created reference dictionaries for use in retrieving data. (See working example below)
First, in order to use scripting dictionaries in VBA, go to Tools > References and check the box next to "Microsoft Scripting Runtime." I learned this here: Does VBA have Dictionary Structure?. I also learned that this setting is included if the sheet is distributed (others won't have to enter VBA and check the box before they can use your sheet): http://www.snb-vba.eu/VBA_Dictionary_en.html.
Public Type ItemTracked
ItemName As String
UseDates As Scripting.Dictionary
End Type
Public Type TrackingStructure
TestLab As String
TrackedItems() As ItemTracked
End Type
Sub Tracking()
Dim TrackingArr() As TrackingStructure
'**************
'Example Data
'**************
'Create array of example dates
Dim DateArray As Variant
DateArray = Array(43164, 43171, 43178) 'Excel date codes for 3/5/2018, 3/12/2018, and 3/19/2018
'Create array of example equipment
Dim EquipArray As Variant
EquipArray = Array("Cooling Pump", "Heating Pad", "Power Supply")
'Create array of example number of pieces of equipment in use
Dim UseArray As Variant
UseArray = Array(0, 1, 2)
'Create array of example test lab names
Dim LabNames As Variant
LabNames = Array("LabABC", "Lab123", "LabDOREMI")
'**************
'Creating and Populating Data Structure
'**************
'Create array of TrackingStructure Type with space to track test labs
ReDim TrackingArr(UBound(LabNames))
'Loop through TrackingArr to populate usage for each test lab
For i = LBound(TrackingArr) To UBound(TrackingArr)
'Record lab name
TrackingArr(i).TestLab = LabNames(i)
'Redimension size of TrackedItems to accomodate example equipment
ReDim TrackingArr(i).TrackedItems(UBound(EquipArray))
'Loop through EquipArray for each test lab
For j = LBound(EquipArray) To UBound(EquipArray)
Set TrackingArr(i).TrackedItems(j).UseDates = New Scripting.Dictionary
TrackingArr(i).TrackedItems(j).ItemName = EquipArray(j)
'Loop through dates and usage for each piece of equipment
For k = LBound(DateArray) To UBound(DateArray)
'Populate date and equipment use
TrackingArr(i).TrackedItems(j).UseDates.Add DateArray(k), UseArray(k)
Next k
Next j
Next i
'**************
'Referencing Data
'**************
'Create and Populate Dictionaries for Use in Referring to Data
Set LabNamesRef = New Scripting.Dictionary
Set EquipArrayRef = New Scripting.Dictionary
For i = LBound(TrackingArr) To UBound(TrackingArr)
LabNamesRef.Add TrackingArr(i).TestLab, i
Next i
For i = LBound(EquipArray) To UBound(EquipArray)
EquipArrayRef.Add EquipArray(i), i
Next i
'Demonstration Print of Entire Data Structure
For i = LBound(TrackingArr) To UBound(TrackingArr)
Debug.Print "Lab Name: " & TrackingArr(i).TestLab
For j = LBound(TrackingArr(i).TrackedItems) To UBound(TrackingArr(i).TrackedItems)
Debug.Print TrackingArr(i).TrackedItems(j).ItemName
For k = 0 To TrackingArr(i).TrackedItems(j).UseDates.Count - 1
Debug.Print TrackingArr(i).TrackedItems(j).UseDates.Keys(k), TrackingArr(i).TrackedItems(j).UseDates.Items(k)
Next k
Next j
Next i
'Access One Example Entry
Debug.Print "Lab Name:" & TrackingArr(LabNamesRef("Lab123")).TestLab
Debug.Print "Equipment:" & TrackingArr(LabNamesRef("Lab123")).TrackedItems(EquipArrayRef("Cooling Pump")).ItemName
Debug.Print "Usage on Date 43164: " & TrackingArr(LabNamesRef("Lab123")).TrackedItems(EquipArrayRef("Cooling Pump")).UseDates(43164)
End Sub

VBA to paste only certain values of cell from one sheet to another

Can some one help me with the below code, what I am looking for is, from sheet "Form" certain values of cells mentioned in 2 sets of Array.
1st set of Array should get copied to sheet "Tracker" C3 onward and second set of array from next cell after the 1set of array ends say EF3 onwards.
whereas now first sett is its pasting from A3 and second from A4. Please let me know in case of any question.
Following is the code which I am using now:
Sub AddEntry()
Dim LR As Long, i As Long, cls
Dim LR2 As Long, j As Long, cls2
cls = Array("C2", "C3", "G2", "G3", "C5", "C6", "C7", "C8", "C9", "C10", "C11", "C12", "C13", "A17", "C17", "D17", "F17", "G17", "H17", "A18", "C18", "D18", "F18", "G18", "H18", "A19", "C19", "D19", "F19", "G19", "H19", "A20", "C20", "D20", "F20", "G20", "H20", "A21", "C21", "D21", "F21", "G21", "H21", "A25", "B25", "C25", "D25", "E25", "F25", "G25", "H25", "A26", "B26", "C26", "D26", "E26", "F26", "G26", "H26", "A27", "B27", "C27", "D27", "E27", "F27", "G27", "H27", "A28", "B28", "C28", "D28", "E28", "F28", "G28", "H28", "A32", "C32", "E32", "G32", "H32", "A33", "C33", "E33", "G33", "H33", "A34", "C34", "E34", "G34", "H34", "A35", "C35", "E35", "G35", "H35", "A39", "D39", "F39", "A40", "D40", "F40", "A41", "D41", "F41", "A45", "C45", "E45", "G45", "A46", "C46", "E46", "G46", "A47", "C47", "E47", "G47", "D51", "D52", "D53", "D54", "D55", "D56", "D57", "D58", "D59", "D60", "D61", "D62", "D63", "D64", "D65", "D66", "D67")
With Sheets("Tracker")
LR = WorksheetFunction.Max(3, .Range("C" & Rows.Count).End(xlUp).Row + 1)
For i = LBound(cls) To UBound(cls)
.Cells(LR, i + 1).Value = Sheets("Form").Range(cls(i)).Value
Next i
End With
cls2 = Array("E51", "E52", "E53", "E54", "E55", "E56", "E57", "E58", "G59", "E60", "E61", "E62", "G63", "E64", "E65", "E66", "E67", "C70", "D70", "E70", "F70", "G70", "H70", "C71", "E71", "G71", "C72", "E72", "G72", "C73", "E73", "G73", "C74", "E74", "G74", "C75", "E75", "G75", "C76", "E76", "G76", "C77", "E77", "G77", "C78", "E78", "G78", "C79", "E79", "G79", "C82", "D82", "E82", "F82", "G82", "H82", "C83", "E83", "G83", "C84", "E84", "G84", "B88", "B89", "B90", "B91", "C88", "C89", "C90", "C91", "D88", "D89", "D90", "D91", "E88", "E89", "E90", "E91", "F88", "F89", "F90", "F91", "G88", "G89", "G90", "G91", "H88", "H89", "H90", "H91")
With Sheets("Tracker")
LR2 = WorksheetFunction.Max(3, .Range("EW" & Rows.Count).End(xlUp).Row + 1)
For j = LBound(cls2) To UBound(cls2)
.Cells(LR, j + 1).Value = Sheets("Form").Range(cls2(j)).Value
Next j
End With
End Sub
Assuming that you want to start cell entries in sheet "Tracker" more to the right, you can add the column number instead of +1 (= column A) and write as follows:
Array 1: assigning cell values starting from column C
.Cells(LR, i + [C1].Column).Value = Sheets("Form").Range(cls(i)).Value
Array 2: assigning cell values starting from column EF
' should be LR2 instead of LR :-)
.Cells(LR2, j + [EF1].Column).Value = Sheets("Form").Range(cls2(j)).Value
Note
[C1].column returns the column number (in any worksheet), e.g. column C Counts 3.
I took a look at your file; the first thing I did was flip through the VBA & try to compile it -- which incidentally, I would recommended to anyone as a first step with a downloaded XLSM. (I haven't seen a malicious macro yet and I'd like to keep it that way!)
I can see that this file has been a "work in progress" because there are bits of code here and there that don't compile properly, such as Me statements pointing to a missing userform, and references to mis-named worksheets such as Form (View) instead of View_Form.
Ideally, this project should be moved from Excel to Access. Excel can be used for filling forms and storing data, but if this is potentially going to sizable, you're best off to use "the right tool for the job". Duplicating your form(s) into Access forms instantly removes the need to copy certain cells to certain sheets, not to mention ease of validation, reporting, security, and unlimited room for expansion plus ease of moving data between Excel, Access, Outlook, etc.
(You even called the spreadsheet a database in one spot!) If your concern is that you're unfamiliar with Access, if you designed this workbook, migration to Access will be a breeze once you figure out the basics of table and form design.
Even Outlook has some pretty nifty form capabilities which can autopopulate the data table when an emailed form is received.
If you need to stay in Excel, how about a User Form instead of the sheet-based form? I too often see people forgetting about Office's built-in features and starting from scratch. That being said, I've been a user of MS Office for 25 years and have never used an Excel User Form. When I think "form", I think MS Access.
Another option, if you want to stay with the worksheet-based form, instead of listing all the cells in the array etc, a minor redesign could make it simpler. One way would be to have a hidden row on the form tab so you have a single uninterrupted line of all the data you need to store. For example, you could hide row 1 and 2, make row 1 the headings like Sourced Processed Year Address etc. and then row 2 could be an "interim" place to store the data, so A2 formula is =C2, B2 is =C3', B3 is=C5` etc.
Finally another sneaky option could be to add hidden comments in each cell that has data that needs to be saved, and then when the form is complete, loop through all the cells looking for comments, and each comment would contain a title or cell reference indicating where that cell's data needs to go.
The destination should be a very straightforward table Use as many columns as you need, but it's not a place for formatting or formulas. (Think database!)
For example, C2 (Sourced By) could have a hidden comment like "Tracker:C" then when the form is filled, you could parse the comments and move the data dynamically (instead of hardcoding 250 cell addresses!) with something like:
Option Explicit
Sub moveData() 'untested; example only
Dim cell As Variant, nextBlankRow As Integer
Dim comm As String, sht As String, col As String
nextBlankRow = 5 'calculate this somehow
'loop through cells with comments
For Each cell In ActiveSheet.Cells.SpecialCells(xlCellTypeComments)
If cell.Comment.Text <> "" Then
'get comment
comm = cell.Comment.Text
'extract location for data like "Sheetname:Columnletter"
sht = Left(comm, InStr(comm, ":") - 1)
col = Right(comm, Len(comm) - InStr(comm, ":"))
'populate correct location with data
Sheets(sht).Range(col & nextBlankRow).Value = cell.Value
End If
Next cell
End Sub
As with anything in Excel (or Office in General) there are a dozen ways you could accomplish the same task. Opt for the ones that don't involve repeating the same code over and over, nor hardcoded data. Planning for future (unexpected) growth is very important, as is debugging as-you-go, which is my last suggestion:
Option Explicit
at the top of every module, and Alt+DLcompile often, removing or commenting-out unused code.
Bottom line, best bet: Access, Excel, Outlook all have form capabilities built in. use a form for a form and you'll save yourself a headache now and later.
Hopefully this gives you some ideas.
Good Luck!

JOINING TABLES PROPER SYNTAX

I attempting to read in data from a 2nd table to populate a few textboxes on my form.
The two tables involved: LOAD_INFO_TABLE and FUEL_TABLE
I am attempting to join them as such:
Dim taFuel As New IFTAFuelDataSetTableAdapters.Fuel_TableTableAdapter
taFuel.Adapter.SelectCommand = New SqlClient.SqlCommand("SELECT Load_Info_table.PETS_LOAD_NUMBER" & _
"From Load_Info_Table As l" & _
"INNER JOIN Fuel_Table AS f" & _
"ON l.PETS_LOAD_NUMBER = f.PETS_LOAD_NUMB" & _
"Where f.PETS_LOAD_NUMB ='" & tbPETSLoadNumber.ToString & "'", taFuel.Connection)
I'm assuming I also need to create a new dataset to work from with info from both the LOAD_INFO_TABLE and the FUEL_TABLE but unsure how to do that either.
My end goal is I want to be able to load data related to a specific load number which is present and spread out into both tables. Then utilizing the new dataset to populate information on my form for that load number, kind of a "catch-all" page with important data on one form as opposed to needing multiple forms and switching between them.
Specifically I am looking to populate a total fuel cost and gallons purchased onto the load info table. I want to do this to eliminate the need to place the exact same information into both tables. It is redundant.
Further clarification: I want to be able to input or step through the Loads or view a specific Load (2017001 as an example), and the load information screen brings in all data requested from the LOAD_TABLE and FUEL_TABLE. I also want to bring over data from the FUEL_TABLE and populate specific text boxes on the Load screen. JOINING is NOT a strong suit of mine yet I know this can be done but I just do not fully comprehend how to syntax the porting of the data into the form using inner join.
I have spent days watching videos and reading information but nothing assists me in my specific need, or I am not understanding it if it does. This is a last resort request.
The SQL query you are pasting together is invalid (no whitespace between "lines"). And you must use the table aliases in the SELECT clause.
It's much easier if you use VB's new multi-line string literals and parameters. Then your SQL queries can be tested verbatim in SSMS.
EG
Dim sql = "
SELECT l.PETS_LOAD_NUMBER
From Load_Info_Table As l
INNER JOIN Fuel_Table AS f
ON l.PETS_LOAD_NUMBER = f.PETS_LOAD_NUMB
Where f.PETS_LOAD_NUMB = #loadNumber
"
Dim cmd = New SqlClient.SqlCommand(sql, taFuel.Connection)
cmd.Parameters.AddWithValue("#loadNumber", tbPETSLoadNumber.ToString)
taFuel.Adapter.SelectCommand = New SqlClient.SqlCommand(sql, taFuel.Connection)

What is the best way to give last elements/items of DB table

In my VB code I am reading all the news from database, there are hundreds of them:
For Each lnews In ltable.Rows
Dim litem = lcontent
litem = litem.Replace("%Headline%", _
Web.HttpUtility.HtmlEncode(lnews("Headline")))
Next
I need to get just 3 latest ones. What is the best way to do it?
For i As Integer = 0 To 2
Dim s = ltable.Rows(i)
Next i
I'm not sure about definition of "the best way" here, but you can get last 3 rows from DataTable using LINQ like this :
For Each s In ltable.AsEnumerable().Skip(ltable.Rows.Count() - 3)
's is a DataRow here
Next
or without LINQ :
Dim count = ltable.Rows.Count()
For i As Integer = count - 4 To count - 1
Dim s = ltable.Rows(i)
Next i
You should probably avoid returning more rows than necessary from your database wherever possible, there is no point in returning 100+ rows of data if you only require the use of the top 3 as it will just increase the query time as you get more and more headlines in your database.
If you do need them all to be returned but just require the first three (or last three) then you can use LINQ as already posted by har07.

Resources