I'm trying to write some VBA code to generate a text file containing SQL INSERT statements for all records in a table in an Access database (accdb). The table has an OLE Object field and a Binary field. I can't seem to get them written to the text file properly; I mostly get question marks(?). I've search for solutions and found some possible ideas but none worked.
If any one has suggestions, I will be very appreciative of any help that you can provide.
Miguel
I actually found an solution following some more searching:
Function ByteArrayToHex(B() As Byte) As String
Dim n As Long, I As Long
ByteArrayToHex = Space$(2 * (UBound(B) - LBound(B)) + 2)
n = 1
For I = LBound(B) To UBound(B)
Mid$(ByteArrayToHex, n, 2) = right$("00" & Hex$(B(I)), 2)
n = n + 2
Next
ByteArrayToHex = "0x" & ByteArrayToHex
End Function
Michael
To export OLE objects in a Microsoft Access file, open this file with the Access application and create a corresponding form with all relevant fields. Use the VBA code provided in the link and you should be able to export some of the most common file types in an automated fashion. Good luck.
https://medium.com/#haggenso/export-ole-fields-in-microsoft-access-c67d535c958d
Related
I'm using ADO because I want to write to a Sharepoint list using VBA in Excel.
Right now I am getting "The Microsoft Access database engine could not find the object 'Isaac Test Excel To Sharepoint', and the code errs on the INSERT line. I suspect it is because of either my site reference being wrong, or my list ID being wrong.
I don't think my list ID is wrong, because I carefully followed the directions to extract the list ID from the URL that's exposed when you go to List Settings, carefully Replacing the 3 replaceable items as mentioned here: https://community.nintex.com/t5/Community-Blogs/Obtaining-a-list-id-in-SharePoint-2010-or-2013/ba-p/77664#:~:text=Navigate%20to%20the%20list%20and%20click%20List%20Settings.,Guid%20Format%20with%20URL%20encoding).
I am passing it in as:
strSharepointListID = "{3404D534–10CB–4F53–BB9D–37F5612155F1}"
I would like to have concluded, "the connection is correct because the code doesn't err until all the way to the INSERT statement", but unfortunately I've proved that to be false: If I pass in a totally non-existent Site value, gibberish, the code still doesn't err until all the way at the INSERT statement.
The name of my list is definitely Isaac Test Excel To Sharepoint
The site I am passing is like this, with me sanitizing this by replacing some text with "text": (I've tried all 3 of these):
strSharepointSite = "https://text.text.text.com"
strSharepointSite = "https://text.text.text.com/sites/text"
strSharepointSite = "https://text.text.text.com/sites/text/_layouts/15/start.aspx#/"
Full code:
Sub Upd2KPIMember_SP()
Dim cnt As ADODB.Connection
Dim mySQL As String
Dim strSharepointListID As String, strSharepointSite As String
'https://community.nintex.com/t5/Community-Blogs/Obtaining-a-list-id-in-SharePoint-2010-or-2013/ba-p/77664#:~:text=Navigate%20to%20the%20list%20and%20click%20List%20Settings.,Guid%20Format%20with%20URL%20encoding).
'list ID from sharepoint URL:
' %7B3404D534%2D10CB%2D4F53%2DBB9D%2D37F5612155F1%7D
'list ID after replacing as follows:
' %7B3404D534%2D10CB%2D4F53%2DBB9D%2D37F5612155F1}
strSharepointListID = "{3404D534–10CB–4F53–BB9D–37F5612155F1}"
strSharepointSite = "[sanitized for SO post]"
Set cnt = New ADODB.Connection
With cnt
.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;DATABASE=" & strSharepointSite & ";LIST=" & strSharepointListID & ";"
.Open
End With
mySQL = "insert into [Isaac Test Excel To Sharepoint] (column1,column2) values ('col1_val1','col2_val1');"
cnt.Execute (mySQL)
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
End Sub
I'm also fairly sure the SQL syntax is good, because the code DID tell me when it was wrong--When at first I used INSERT TABLE instead of INSERT INTO TABLE.
Partial credit to KeshavSharma (see comments) for
correctly mentioning LIST ID not required, use LIST NAME instead
inspiring me to keep focusing on THAT line of code - that it was the problem
The final code that worked was precisely identical to the first code I posted, EXCEPT, inside the connection string, instead of:
LIST={8F7FEF30–C868–4480–8AC8–4FE4FDB3921A};
I need to use:
LIST=Isaac Test Excel To Sharepoint;
(despite spaces in object name - I need to use NO single quotes, NO brackets).
Very happy this got solved - hopefully it helps someone else some day.
I have a database that contains the data as in the following picture
Notes:
Collation Database ('SQL_Latin1.General_CP1_CI_AS').
I have no right to change Collation to Araic_CI_AS.
In case you change Collation to Araic_CI_AS, the data display from the database is displayed in the new program, but the program has a problem and it appears in the old program in the form of ????? Where the old program.
I can not modify the old program because there is no source code for it.
This database is outdated and may not be modified by anyone ( Collation [rows] or Collation [databases])
When you link the database to a new standalone program through Visual Basic 2015 to use some data from the database, the following format appears as in the picture...
What I want is to display the content of the database to change the (ãßÇÆä æÂáÇÊ) sentence to the default language which is Arabic.
I hope to find a solution through the Visual Basic code and not through the amendment to the database.
Thanks to all
The problem has been solved in the process of converting the encoding characters by the following code:
Private Function conv1256(ByVal txt As String) As String
Dim dic As New Dictionary(Of String, String)
Const _1256 As String = "ÐÏÌÍÎåÚÛÝÞËÕÖØßãäÊÇáÈíÓÔÙÒæÉìÑÄÁÆøºÅñõðó¡ÜÃòö¿Âú"
Const _utf8 As String = "ذدجحخهعغفقثصضطكمنتالبيسشظزوةىرؤءئّ؛إًٌَُ،ـأٍِ؟آْ"
For i = 0 To (_1256.Length) - 1
dic.Add(_1256.Chars(i), _utf8.Chars(i))
Next i
For Each ch In txt
conv1256 &= If(dic.ContainsKey(ch), dic.Item(ch), ch)
Next
End Function
To be used this way:
MsgBox(conv1256("ÈÓãö Çááå ÇáÑøÍãä ÇáÑøÍíã"))
Good luck and thank you all
I am working in as a business developer, however the problems in our support department sometimes require me to work on clients' CSV file i.e. Excel spreadsheet containing product catalog.
Right now I have an Export CSV from an online store, where the first column is formatted in the following way:
Name_of_the_product - {store category)
e.g. Sony NEX-6 - {digital camera}
I need a macro/script to take cut the words between {} and paste it into the column immediately to the right, without the {}. This way I will have clean product name i.e. Sony NEX-6, as well as the store category for each product e.g. digital camera (which I need to push the product feed via a third party solution).
Any help would be greatly appreciated!
I have managed to get it mostly working using the following function:
=MID(A2;FIND("[";A2)+1;FIND("]";A2) - FIND("[";A2) - 1)
The only thing now left to do is to delete what was copied from the A column (including the []) - does anyone have the idea for the macro? I hope I can get it working using a Search/Replace functionality from a feed management solution, but I am checking with our support for that.
Thank you all!
I assume your data in column A. And have 100 rows.
Dim WrdArray() As String
Dim text_string As String
Dim LeftSide As String
Dim RightSide As String
For i = 1 To 100
text_string = Range("A" & i).Value
WrdArray() = Split(text_string, "{")
LeftSide = WrdArray(LBound(WrdArray))
RightSide = WrdArray(UBound(WrdArray))
Range("A" & i).Value = LeftSide
Range("B" & i).Value = Replace(Result, "}", "")
Next i
And if you want to understand the split function, you can refer this blog post.
http://www.excelvbasolutions.com/2015/06/split-function.html
I sometime extract plenty of data from a very old database (MS Access). The serialized output is stored as YAML files and these files are locally used by other scripts to speed up the process.
Sometimes we to an update of the local files by extracting the (possible) new data from the database. This extraction is quite long and I would like to avoid it if the content of the concerned tables is the same as the last extraction.
Is it possible to get a sort of signature of the state of a table, or part of the table ?
In other words this would help understanding my question:
signature = db.GetSignature('SELECT * FROM foo where bar = 1')
if local_foo.signature != signature:
local_foo = db.Extract('SELECT * FROM foo where bar = 1')
What solutions could I use?
Using triggers
If you have the luxury of controlling the insert/update/delete functionality of the original Access database, the best/safest solution would be to implement database triggers to enable tracking. That way you could easily at least store a "last modified" value or keep a table that is responsible for storing extensive tracking information.
Unfortunately Access doesn't support triggers (unless you're using 2010+, see below), but you could implement triggers using VBA in the database.
Access 2010 introduced data macros, but I don't think that's an option here!
Using the scripting language
If you can't use database triggers, perhaps you could use a workflow like this:
Execute query and get entire result (single collection)
Turn the query result/collection into a JSON string (e.g. json.dumps() in Python)
Get a hash of the JSON string (e.g. hashlib.sha1() & hashObject.hexdigest() in Python)
Compare hash against the last stored hash of the query result.
Using VBA
To keep things database-side (to avoid transferring data), it could be useful to try generating the hash using VBA in the Access database.
You could use the hashing algorithm code mentioned in this SO post: https://stackoverflow.com/a/14749855
Example:
Using this SHA1 code: https://gist.github.com/anonymous/573a875dac68a4af560d
Dim d As DAO.Database
Dim r As DAO.Recordset
Dim s As String
Set d = CurrentDb()
Set r = d.OpenRecordset("SELECT foo, bar, baz FROM foobar")
s = ""
While Not r.EOF
s = s & r!foo & "," & r!bar & "," & r!baz & ";"
r.MoveNext
Wend
r.Close
Set r = Nothing
Set d = Nothing
s = SHA1TRUNC(s)
I'm relatively new at VBA, and I am developing this excel based tool that creates two .xls files, whose paths are saved in two variables. The next step is the user has to select these two files, right click and select an application called Compare.
I was wondering if this process could be automated using Shell commands in VBA. Please advise.
Yes, it is possible. Assuming the compare takes two files as arguments, just do something like this:
Public Function OpenCompare(strFileOne As String, strFileTwo As String)
Dim x As Variant
x = Shell("Compare.exe " & strFileOne & " " & strFileTwo, vbNormalFocus)
End Function