MS Access setting max length of a string in a query - vba

I'm having an issue setting the max length of a username field that is the result of a query. The query takes an employees first name's letter, their last name and combines them.
ie, Bob Smithson = bsmith or Danny Charlesworth = dcharl
I understand that people have long names and to limit the username to something like 6 or 7 characters would make logins less tedious.
Here's my query code:
Username: LCase(Left([first],1) & [last])
Pretty simple, but I cannot find any maxlen or left == 7 for example. I could do a work around and do a for loop and knock off a letter on the right until it matches 7, but there must be a faster way to do this. The query populates the form in real time and makes it look nice. Also I don't know how to edit the field of a query textbox since the form would update on load.
Thanks

Username: LCase(Left([first],1) & Left([last],5))
with regards to this question "Also I don't know how to edit the field of a query textbox since the form would update on load."
Go to query design,
under the field name, do the following
Delete the field name
write "New field name:LCase(Left([first],1) & Left([last],5))"
in your case, the new field name will be user name.

Related

Searching for string data in multiple rows

Good morning,
I put together a statement that works with single string searching. I wanted to know if its possible to retrieve multiple string data from different rows in a single search. I created the statement on sql management studio, I then modified it so in our program it prompts the user to enter the order number they are looking for in the row.
SELECT HID, INumber, SlsrdrNmbr, ShpNm, CustomerNumber, InvoiceDate, BlncD,Comments
FROM dbo._adhoc_Inv
WHERE BillNumber = '512315'
AND Comments LIKE '%{{enter D}}%'
prompt pops up
user enters number D-12313
Ideally I would like for them to enter multiple numbers, because they need to match the D numbers with our internal set of the numbers we have in our system.
So this works just fine, brings up the data I want. I want to make it easier by showing multiple search results so they don't need to close the window and search again. Is is possible to do what I am looking for? I am still a bit of a novice.
I would like to add if I add multiple D numbers that are on the SAME row, results will show since its the same row being retrieved.
Thank you

Matching VBN Textbox Text with Autonumber With Custom Format in Access

I have an access database where "Orders" is my table with the column name CusID and is set to Autonumber with format "CUS"0001
I'm trying to read an autonumber with the custom format "CUS0001" from VBN but I can't seem to read it.
I've tried to read it all as a string, but I can't seem to read it.
cmdCustomer.CommandText = "Select * From Orders Where CusID = " & (txtCusID.Text) & ";"
Any help would be greatly appreciated! Thank you :)
As the name suggests, AutoNumber values are numbers. "CUS0001" is clearly not a number so clearly cannot be stored in that column. When you specify a format in Access, that relates ONLY to how the Access application displays that data. It says nothing about how the data is stored. If Access displays a value in that column as "CUS0001" then the column actually contains the number 1 and that is all that your VB app will see, so that's how you have to query it. Also, if you want the value displayed as "CUS0001" in your app then YOU are going to have to format it that way.
It's also worth noting that, if you really did want to search for "CUS0001" then you'd have to wrap that value in single quotes in your SQL code or else you're going to get a syntax error. That said, it shouldn't matter because you should be using a parameter to insert that value into your SQL code.

Turn string variables into numeric representatives and store the strings elsewhere?

I don't know the best way to describe my problem and I'm just looking for a push in the right direction, or where to start. I'd be perfectly happy with an answer that's a very useful link or pseudo code.
My problem, I have a database that's about to hit the MS Access hard coded 2 GB database limit and I don't want to split the database.
What I think is a possible solution - make the database more efficient in it's data storage. I think, but don't know if this is true, that I could do this by turning some string fields into numeric fields. Stay with me...
For instance:
My database has several million records of a field we'll call TooLongString
Each value is about 50 characters
Every record has a value for this field
There's only 9 possible values for TooLongString
Would it decrease my database size to instead store a number that
represents one of the 9 possible values and store the text value in a small table? (So go from 50 characters to 1 character several million times)
Did I explain my issue correctly? Is my potential solution actually a solution? How would I go about doing this?
Thanks!
The short answer is yes, that would reduce the size of your database. You could have a second table that holds the nine possible values for "TooLongString" and just store the ID of the appropriate answer in the main table, as you suggested. You would then need to join these tables when pulling the data out in order to retrieve the actual text instead of the ID.
I would set up your new table first, then add a new column for the ID into your existing one. As there are only nine possible values, I'd be tempted to just manually run an UPDATE query nine times, e.g. if the first string in your new table is "MyFirstString" with ID 1, you could run "UPDATE existingTableName SET newColumn = 1 WHERE oldColumn = 'MyFirstString'". Do this for each of the nine values then you can remove the old string column from your table at the end.

How to make my own primary key as id with letters [duplicate]

The title of this Question may not be accurate because I wasn't sure how to ask the question.
Is there A way to have an ID field in AC 2010 the has a constant part and then a part of the ID that the user will enter in?
EXAMPLE: "EMP9066"
-I would like the "EMP" part of the ID to be constant at all times and the user should not be able to change it and the "9066" is a four digit that the user will be asked to type in.
Please Help.
_ Remember this is not SQL just basic access with some macros.
Thanks
Access has a data type called autonumber which will generate a unique number for each record automatically but it does not allow for the alpha prefix.
if it is indeed constant then the simplest approach is to prefix with it for display, ie in the table the field would be called recId (for example) and you would view the rows via a query with a calculated column
EmpId: "EMP" & format$(RecId,"0000")

MS Acess 2003 - VBA for Update SQL query?

hey guys, could someone show me the simple update query through vb? I need to add new fields to the table (just 3) and add a couple text boxes on a form so that users can add some additional data relative to the record (which is already what this form is based on).
So the first form I have is a form that populates a list, when the user double clicks on a selection from that list, it opens a new form, so that the ID of the the table that is tied to this form that I need to add the these text boxes on (all the combo boxes and text boxes relative to one record are tied to the active form at this point, however there are all unbound. On a button click there is already vb that saves the information to the table). I did not create this however, it was built by someone who is not there anymore, and apparently is better than I at this stuff. My problem is that there is soooo much vb that checks for records, and various sql statements based on case, that I cannot decipher it to its simplest form.
So I was looking for a simple example of an update sql statement in vb so I can try to break this apart.
I need it to update the record based on the ID: sql WHERE RecordID = me.RecordID
I actually thought I knew how to do this based on examples, however every time I try, then try to run on button click, I get a run-time error of SYNTAX error, and the debug just highlights the db.execute(sql) part. So I tried to get the resulting immediate window of the sql statement, and it looks fine to me:
UPDATE tblMain
SET [Name] = "John Doe",
[DATE] = #9/30/2009#,
[TYPE] = "TypeA",
WHERE RecordID = 958;
Can I update a table without accounting for every field in the table (because this one has about 15 plus the new 3, so I am ignoring about 14 fields here, but I do not want to alter those anyway???
So as always, I appreciate the help yall!! Thanks!
EDIT:
Sorry I always forget this....I was actaully trying it DAO....
Dim db as DAO.Database
Dim sql as String
set db = CurrentDb
etc
You were thaaat close! You have a simple extra comma after your last column. Get rid of it and it works fine.
UPDATE tblMain SET
[Name] = "John Doe",
[DATE] = #9/30/2009#,
[TYPE] = "TypeA"
WHERE RecordID = 958;
Yes, you can absolutely update only a few columns rather than all of them. That is a best practice, BTW.
Finally, It's considered bad practice to name your columns after reserved words like "Name" and "Date", but I know you inherited this.
You were wise to include Debug.Print sql in your code. bpayne already pointed out the extra comma in your SQL statement.
I want to point out another trouble shooting technique you may find useful to debug SQL statement problems.
Copy the statement from the Immediate Window, and paste it into the SQL View of a new query. Modify the query in the query designer until you can get it working, then revise your VBA code to generate a matching SQL statement.
In this case you might not have noticed the extra comma. However, you could create another new query and build the UPDATE statement from scratch in the query designer. After getting that one working, you could compare its SQL View to the failing query.