Substitute CHAR(150) in MS Access - ms-access

I have MS Access Table fields which has values CHAR(150) - and I like to replace it with CHAR(45) -. HOw can I do in MS Access query ?
I tried below in Query but SUBSTITUTE does not exist. It works great in Excel
Replace(SUBSTITUTE("Value1",CHAR(150),CHAR(45))," ","")

Replace does substitution. Chr() is Access function.
Replace([fieldname], Chr(150), Chr(45))

Related

MS Access InStr Trim #FUNC! Null

Using Trim and InStr, I have written an MS Access query to extract data from a field. The query works as designed to extract the desired data, however I receive a #FUNC! error when the field I am extracting from is blank.
I have attempted nz and an IIF statement - neither worked.
Current code:
ExprA: Trim(Left([Target],(InStr(1,[Target],"=")-1)))
Any ideas on how to edit this query to prevent the #FUNC! error from appearing for blank/null values?
Thanks!
You can "cheat" a little:
ExprA: Trim(Left([Target],(InStr(1,[Target] & "=","=")-1)))
You can check if [Target] contains "=" with IIF():
IIF(
InStr(1,[Target],"=") > 0,
Trim(Left([Target],(InStr(1,[Target],"=")-1))),
[Target]
)
This code will return the whole [Target] column's value if [Target] does not contain "=" and will not throw an error if the column is NULL.

How do I change this SQL query to work in an MS Access query?

How do I change the following SQL query to Access to make it work?
WHERE s.SchoolName like '%' + A.[Choice_School] + '%'
I'm trying to look up one column to see if it's contained in another column and this SQL gets me the results I'm after, I just don't know how to concatenate a LIKE operator on a column in Access.
Thank you for your help.
In MS Access, the wildcard character is the asterisk (*).
Double quotation marks and asterisk is the Access wildcard. As follows:
WHERE [s].[SCHOOLNAME] LIKE "*" & [a].[CHOICE_SCHOOL] & "*";

MS Access CREATE TABLE "WITH COMPRESSION" syntax?

I'm trying to write a CREATE TABLE statement for Microsoft Access (to be executed via a C# / .NET app using an OleDbConnection), utilizing the WITH COMPRESSION attribute to cause character columns (TEXT) to be created using single-byte characters rather than Unicode double-byte characters, as documented on MSDN here.
The WITH COMPRESSION attribute can be used only with the CHARACTER and MEMO (also known as TEXT) data types and their synonyms.
The WITH COMPRESSION attribute was added for CHARACTER columns because of the change to the Unicode character representation format. Unicode characters uniformly require two bytes for each character. For existing Microsoft® Jet databases that contain predominately character data, this could mean that the database file would nearly double in size when converted to the Microsoft Access database engine format. However, Unicode representation of many character sets, those formerly denoted as Single-Byte Character Sets (SBCS) can easily be compressed to a single byte. If you define a CHARACTER column with this attribute, data will automatically be compressed as it is stored and uncompressed when retrieved from the column.
When I try to execute the following statement (which I believe to be syntactically correct per MSDN) via an OleDbConnection, I get a syntax error.
CREATE TABLE [Foo] ([COL1] TEXT(255) WITH COMPRESSION)
Likewise, executing the same statement directly within MS Access 2013 as a query gives a syntax error at WITH.
Executing
CurrentProject.Connection.Execute("CREATE TABLE [Foo1] ([COL1] TEXT(255) WITH COMPRESSION)")
from Access VBA does work, however.
If I take out the WITH COMPRESSION attribute, the statement executes without error both via OleDb and directly in MS Access.
Any ideas what I'm doing wrong?
My problem turned out to be a syntax error that wasn't reflected properly in my original question.
However, solving that problem revealed that the documentation for MS Access CREATE TABLE on MSDN https://msdn.microsoft.com/en-us/library/office/ff837200.aspx is incorrect regarding the sequence of attributes for the CREATE TABLE statement. According to the documentation, the syntax is:
CREATE [TEMPORARY] TABLE table (field1 type [(size)] [NOT NULL] [WITH COMPRESSION | WITH COMP] [index1] [, field2 type [(size)] [NOT NULL] [index2] [, …]] [, CONSTRAINT multifieldindex [, …]])
but in fact, [WITH COMPRESSION | WITH COMP] must appear before [NOT NULL] or you get a syntax error.
Additionally, it's not possible to execute the CREATE TABLE statement using the WITH COMPRESSION attribute from a query directly within MS Access. You have to either use VBA or (as in my case) an external program via OleDbConnection.
My experience with "WITH COMPRESSION" and MS-ACCESS 2013
Impossible to run such a script from query window.
Possible from VBA but with limitations:
currentdb.Execute "... WITH COMPRESSION" -> "Syntax error in CREATE
TABLE" CurrentProject.Connection.Execute " ..." - > Ok
I confirm what "Mr. T" says: WITH COMPRESSION must appear before NOT NULL

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?

How to store a String (length > 255) from a query?

I'm using Access 2000 and I have a query like this:
SELECT function(field1) AS Results FROM mytable;
I need to export the results as a text file.
The problem is:
function(field1) returns a fairly long string (more than 255 char) that cannot be entirely stored in the Results field created from this query.
When i export this query as a text file, i can't see the string entirely. (truncated)
Is it possible to cast function(field1) so it returns a Memo type field containing the string ?
Something like this:
SELECT (MEMO)function(field1) AS Results FROM mytable;
Do you know others solutions?
There is an official microsoft support page on this problem:
ACC2000: Exported Query Expression Truncated at 255 Characters
They recommend that you append the expression data to a table that has a memo field, and export it from there. It's kinda an ugly solution, but you cannot cast parameters to types in MS Access, so it might be the best option available.
i don't know how to do quite what you're hoping (which makes sense) but a possible alternative could be to create 2 or 3 fields (or separate queries) and extract different portions of the text into each then concat after retrieved.
pseudo: concat((chars 1-255) & (chars 256-510) & (chars 511-etc...))
edit: it's odd that a string longer than 255 is stored but it's not memo. what's up there? another alternative, if you have access to the db, is change the field type. (backup the db first!)