Determining Encoding in MS Access Database - ms-access

I have a MS Access database with a column that has some strange encoding. Oddly, I am unable to copy/paste this into anything (Chrome, Word, etc), because it strips out most of the unicode characters (though not all of them). What I am wondering, is there a way to determine what type of encoding is being used here?
Somehow the program I am using is taking this column and decoding it to readable text. I converted the Access database to PostgreSQL on a Linux system, but I'm pretty sure whatever encoding is being used here did not map correctly into the PostgreSQL database. What I'm trying to do is to convert this to hex, but I cannot do it since I'm unable to copy/paste the characters out of the database.

You can open the table as a recordset. Then loop the records and convert the field to hex using a function like this:
Public Function StrToByte(ByVal strChars As String) As String
Dim abytChar() As Byte
Dim lngChar As Long
Dim strByte As String
abytChar() = StrConv(strChars, vbFromUnicode)
strByte = Space(2 * (1 + UBound(abytChar) - LBound(abytChar)))
For lngChar = LBound(abytChar) To UBound(abytChar)
Mid(strByte, 1 + 2 * lngChar) = Hex(abytChar(lngChar))
Next
StrToByte = strByte
End Function
Or create a query:
Select *, StrToByte([EncryptedFieldName]) As HexField
From tblYourTable

Related

Access query recordset result output to word shows special characters

Problem Description:
I am using Microsoft Access Plus 2010, with below code to export the result of query to Word table. However, there are all kinds of special characters exported if the record is over 255 characters.
Below are the query, VBA etc.
Query name: Qa
Query function: select field from Ta
VBA:
Dim qbf As QueryDef
Dim dabase As Database
Set dabase = CurrentDb
Set qdf = dbase.QueryDefs(Qa)
Dim results As Recordset
Dim flds As String
Set results = qdf.OpenRecordset()
While (Not results.EOF)
doc.addRecord results
results.MoveNext
Wend
qdf.Close
Public Sub addRecord(pubRecordSet As Recordset)
flds = pubRecordSet.Fields("fieldname")
mTable.cell(1, 1).range.InsertAfter (flds)
...
End Sub
Where 'mTable' is a Word table object, 'fieldname' is the name of the field to be exported to Word Table.
This VBA in general works fine when the length of flds is less than 255, however, it throws a lot of special characters in the Table cell if the length exceeds 255.
Example on special characters exported to Word table cell:
退D瞻껙皿 Ƭ" " ᬈ௩Hȷ⫗ 鋨D૝૝ィ௨瞻껥皿௲Ǭ" "Tೕ ŮԱ ࿨ซ鐌D
I checked the limitation of MS Access from link here. It mentions the recordset of query is 1GB, which my data is way less (~255 characters). Any help is appreciated.
I think they are being truncated or corrupted almost certainly to do with one of the reasons listed here : http://allenbrowne.com/ser-63.html
By definition if they are over 255 character long they will be interpreted as Memo or Long text (Same definition - Memo is the older name of the data type).

How do I get system.byte to MySQL Bit type?

I have a MySQL database (5.6 Community) with a column of type bit(60). The field holds values such as 1001, 0011, etc. I am trying to pass a string of "1010" to the database through a vb.net adapter. If I use a regular query it would look like this:
insert into my_table (my_bit_field) values (b'1010');
This works and inserts the string exactly as shown but I need to use a data adapter so I can't send the query directly.
When using the data adapter in vb.net, I was getting an error saying that it was expecting a byte array. So I tried using that:
System.Text.Encoding.ASCII.GetBytes("1010")
but it just converted it to its ASCII representation of bytes (49,48,49,48).
Is there a better way to go through the data adapter and is there a way to do this?
Thanks.
In this case, you could try the following to convert your string to a byte array:
Dim bytes() As Byte = { Convert.ToByte("1010", 2) }
However, that breaks once you have more than 8 bits in your string. You could (perhaps should) break the string into byte-sized sections and convert each to a byte, such as for this question. Since you have a bit(60) column you could also cheat a little and use something like this:
Dim inputValue As String = "000100000010000000110000010000000101000001100000011100001000"
If inputValue.Length > 60 Then ' up to 64 could be supported
' Error
End If
Dim longValue As ULong = Convert.ToUInt64(inputValue, 2)
Dim bytes() As Byte = BitConverter.GetBytes(longValue)
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
This will give you the byte array that you can presumably use in your data adapter code that was not shown.

Lost decimals in MySQL

I have a MySQL InnoDB table, one of the columns is defined as double and have stored a number 16155.987841701322 but if I perform a query from VB.NET returns
16155.9878417013
From MySQL Workbench I can see all the digits in the DB, but from VB:NET i get two digit less.
Why didn't return all the digits?
I make a little code to perform conversion test
Dim OdometerDbl As Double = 16155.987841701322
Dim OdometerStr As String
OdometerStr = CStr(OdometerDbl)
After this I get 16155.987841701322 in OdometerDbl and 16155.9878417013 in OdometerStr; so is a VB.NET Double to String conversion issue.
Theres any way to get around this or a better way to convert a Double to String without losing decimals?
From the example above
OdometerStr = OdometerDbl.ToString("R")
or
OdometerStr = OdometerDbl.ToString("G17")
References:
Why Is ToString() Rounding My Double Value?
VB.NET Converting Double Value to String = Precision loss

What are the String, LongText and ShortText lengths? Setting Long Text as SQL Parameter results in Error 3001

When I create an append Query in ms-access 2013 with parameters and any of those parameters' type is set to LongText the query fails with error code 3001 Invalid Argument. Changing the type to ShortText however results in a working query. Both version are runnable by double clicking the query in access itself, but the first one fails when running it via following code:
Dim db As DAO.Database
Set db = CurrentDb
Dim qdf As QueryDef
Set qdf = db.QueryDefs("NeuerFachlicherInhalt")
qdf!Inhalt = inhalte("DefaultInhalt")
qdf!Formular = inhalte("Formular")
qdf.Execute
The table I insert the parameter to has a field type of LongText and therefore I would expect this to work - what is the root cause of the issue here? And how can I pass in a long text if I am unable to specify a LongText as parameter?
I think it might be connected to the length limitations of Strings in access. What exactly are those limitations? Google redirects you to concatenation and max length of string in VBA, access regarding the question for string lengths, but i can not find a definite answer to the length question(s):
how long can the text for ShortText be?
how long can the text for LongText be?
how long can the text for a vba String be?
My queries in the two cases look like
PARAMETERS Inhalt LongText, Formular Short;
INSERT INTO FachlicherInhalt ( Inhalt, Formular )
SELECT [Inhalt] AS Expr1, [Formular] AS Expr2;
PARAMETERS Inhalt Text ( 255 ), Formular Short;
INSERT INTO FachlicherInhalt ( Inhalt, Formular )
SELECT [Inhalt] AS Expr1, [Formular] AS Expr2;
ShortText (simply Text prior to Access 2013) can be up to 255 characters in length.
LongText (Memo prior to Access 2013) can be up to 1 GB in length, but most Access controls can only display 64000 characters. (A Textbox in a Form will start behaving weird when editing the text, if it contains much less than those 64000 characters.)
See the Access 2013 Documentation for further details.
A VBA variable-length String can be up to 2^31 characters
See the Visual Basic for Applications Language Reference for further details.
Now for your question regarding the LongText-Parameter in the QueryDef-Object. Unfortunately DAO does not support LongText as Parameter-Type for a Query even though it lets you create the parameter in query design.
You have got the following options as a workaround:
Open a recordset and add/update the record there
Use an ADO-Command-Object for that query
Hardcode your function inhalte("DefaultInhalt") into the SQL of the query
Or concatenate your own SQL string including the values (Total SQL lenght limited to 64000 characters!)
So long as I am reading your question correctly, I'm almost certain you can't use a longtext/memo field as a parameter. As per the information found here: Any way to have long text (memo) parameters in DAO and MS Access?

Vb.net read settings file

I created a settings file for my application, like this:
username: ProGamingHun
version: 1.0
maxmemory: 1GB
minmemory: 512MB
I want to read username, so Dim username = in the settings file: ProGamingHun, how can i do this? The username is unknow leght, because ProGamingHun is a test text. Thanks for helping.
I'm not convinced, the code you posted as answer, parses the file correctly. You're better off, using regular expressions instead:
Dim lines() As String = IO.File.ReadAllLines(SettingsRoot + "\config.cfg")
Dim matches = lines.SelectMany(Function(line) Regex.Matches(line, "(.*): (.*)").Cast(Of Match))
Dim dictionary = matches.ToDictionary(Function(match) match.Groups(1).Value, Function(match) match.Groups(2).Value)
dictionary will now contain the key-value pairs of your settings. Even though this wasn't your original question, you can display them in a message box one by one with the following code:
For Each setting In dictionary
MessageBox.Show(setting.Key & "=" & setting.Value)
Next
Nevertheless, I'd suggest you use one of many standard formats for saving settings. This way, you can use existing libraries to parse them:
XML: ConfigurationManager class
JSON: Json.NET
INI: INI File Parser
EDIT
To get values from the dictionary into individual variables, use dictionary.TryGetValue which handles missing keys (settings):
Dim username As String = Nothing
dictionary.TryGetValue("username", username)