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.
Related
Recently I have been using Microsoft SQL for creating databases that are referred to using an excel document. There have been a number of instances when I needed to make a small changes to my tables and ended up "DROP"-ing all my current tables and re-creating them using an updated query. I understand you can use UPDATE to change the values of records within a table, but I'm looking to manipulate a data type so that I can change the number of decimals in one record of my tables from 2 to 3. Code for creating the table looks something like this:
CREATE TABLE WIRE_INDEX
--"Field" "Data Type" "Null or Not"
(...
...
DENSITY decimal(18,2) Not Null);
I don't know if the solution is something obvious, but I have been unable to find anything useful. I'm not sure know how to refer to the data type of a field in SQL.
When I populate the database I use numbers like 0.283 and 0.164, but when I SELECT the record I only get the first two decimals. I'd like the first 3 decimals to appear in the way I enter them into the table.
(edit didn't show up properly)
(not sure if I'm supposed to post my solution), but credit to TEEKAY and Apurav for answering my question. I used the code posted by Apurav which looks like this:
ALTER TABLE WIRE_INDEX
ALTER COLUMN DENSITY decimal(18,3) Not Null
When I pulled the table, using a SELECT statement the precision showed three decimal places, but I lost the precision of my input and had to re-enter my values using UPDATE. Not sure if this is more effective than just starting over, but it worked for me and now I know.
I am trying to set a default value of a field in an access table in Table Design. The test I entered is:
=DLookUp([Tbl::VII::A::03::b_stkIden].[stkTersNam]","[Tbl::VII::A::03::b_stkIden]","[stkIdx]= [stkIdenOld]") where
In the different attempts the text was typed (did not work), copied and pasted (did not work). Keeps saying Default value or Validation rule is invalid.
stkIden is the source table containing the value I need
stkTersNam is the field which contains the value I need
stkIdx is the index field of my target table in which I want to insert the lookup value
stkIdenOld is the index field in the source table …stkIden.
It seems that default value attribute of tables in Access 2019 no longer accepts any references to any Table in the current database. It seems strange the Ms would remove this essential functionality. Is this just a quirk of the Jet Engine.
Please help. I am desperate. Should I consider abandoning Access for some other more user friendly database.
Should not have 'where' at end of expression. Don't put parameter within quote marks - concatenate dynamic parameter. Missing quote mark for the field argument.
=DLookUp("[stkTersNam]", "[Tbl::VII::A::03::b_stkIden]", "[stkIdx]=" & [stkIdenOld])
If stkIdx is a text field, use apostrophe delimiters.
=DLookUp("[stkTersNam]", "[Tbl::VII::A::03::b_stkIden]", "[stkIdx]='" & [stkIdenOld] & "'")
However, this won't work as a DefaultValue (won't in any version of Access) and really makes no sense to pull related info via DefaultValue property anyhow. Options:
Query joining tables.
Multi-column combobox on form.
DLookup in textbox ControlSource.
I have problems with my records within my database, so I have a template with about 260,000 records and for each record they have 3 identification columns to determine what time period the record is from and location: one for year, one for month, and one for region. Then the information for identifying the specific item is TagName, and Description. The Problem I am having is when someone entered data into this database they entered different description for the same device, I know this because the tag name is the same. Can I write code that will go through the data base find the items with the same tag name and use one of the descriptions to replace the ones that are different to have a more uniform database. Also some devices do not have tag names so we would want to avoid the "" Case.
Also moving forward into the future I have added more columns to the database to allow for more information to be retrieved, is there a way that I can back fill the data to older records once I know that they have the same tag name and Description once the database is cleaned up? Thanks in advance for the information it is much appreciated.
I assume that this will have to be done with VBA of some sort to modify records by looking for the first record with that description and using a variable to assign that description to all the other items with the same tag name? I just am not sure of the correct VBA syntax to go about this. I assume a similar method would be used for the backfilling process?
Your question is rather broad and multifaceted, so I'll answer key parts in steps:
The Problem I am having is when someone entered data into this
database they entered different description for the same device, I
know this because the tag name is the same.
While you could fix up those inconsistencies easily enough with a bit of SQL code, it would be better to avoid those inconsistencies being possible in the first place:
Create a new table, let's call it 'Tags', with TagName and TagDescription fields, and with TagName set as the primary key. Ensure both fields have their Required setting to True and Allow Zero Length to False.
Populate this new table with all possible tags - you can do this with a one-off 'append query' in Access jargon (INSERT INTO statement in SQL).
Delete the tag description column from the main table.
Go into the Relationships view and add a one-to-many relation between the two tables, linking the TagName field in the main table to the TagName field in the Tags table.
As required, create a query that aggregates data from the two tables.
Also some devices do not have tag names so we would want to avoid the
"" Case.
In Access, the concept of an empty string ("") is different from the concept of a true blank or 'null'. As such, it would be a good idea to replace all empty strings (if there are any) with nulls -
UPDATE MyTable SET TagName = Null WHERE TagName = '';
You can then set the TagName field's Allow Zero Length property to False in the table designer.
Also moving forward into the future I have added more columns to the
database to allow for more information to be retrieved
Think less in terms of more columns than more tables.
I assume that this will have to be done with VBA of some sort to modify records
Either VBA, SQL, or the Access query designers (which create SQL code behind the scenes). In terms of being able to crunch through data the quickest, SQL is best, though pure VBA (and in particular, using the DAO object library) can be easier to understand and follow.
In my Access database, I have a table called customers. In this table I have a column called DateEntered. The data type for the field is short text.
The values in this column are not coherent - they come in several variations:
MM-DD-YYYY,
MMDDYYYY and
MM/DD/YYYY.
There doesn't seem to be any standard set.
My goal is to select all customers from 2012. I tried
select *
from customers
where DateEntered <('%2013') AND >('%2012');
but it comes up blank when I run it.
Can anyone point out what I'm failing to do correctly & more importantly explain why exactly this query doesn't work in Access? From my understanding of SQL (not very advanced) this should work.
Another variant)
select * from customers where RIGHT(DateEntered, 4) = '2012'
If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.
One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field.
At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.
Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name.
Once you fix the problem, querying against the field will be a piece of cake.
Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.
If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.
Why don't you use LIKE operators (they're appropriate when you have a pattern using % and _):
select * from customers where DateEntered like '%2013' or DateEntered like '%2012'
Ok, here's the situation
Let's have Table1(A,B,C)
A is an autonumbering field.
I'm feeding the table via vba.
Since A is autonumbering, I'm ignoring it like so:
SQL = INSERT INTO TABLE1(B,C) VALUES ('something','something else')
DoCmd.RunSQL SQL
This works ok, access gives me a 1st warning that I'll be creating a new row.
Which is ok to me. However just after that I get this:
Microsoft Access can't add all the records in the update or append query.
It set 1 field(s) to Null due to a type conversion failure.
blahblahblah click ok to run the query anyway
Which doesn't prevent it from working if I click ok, but I don't want my user to see
that warning.
And anyway why does it pop up ? Isn't it normal to leave the autonumbering field blank ?
Is there another procedure I don't know about ? What am I missing ?
I looked around google and here but couldn't find an answer :/
(I don't want to setwarnings to false since I want the first warning of adding a field and any other eventual error to be visible.)
Microsoft Access can't add all the records in the update or append query.
It set 1 field(s) to Null due to a type conversion failure.
That error has nothing to do with your autonumber field. Check the data types of fields B and C.
In this example, field C is set to number, so I get the same error as you, and the INSERT succeeds but with Null in field C:
DoCmd.RunSQL "INSERT INTO TABLE1(B,C) VALUES ('something','something else')"
However, inserting a numeric value into field C works fine.
DoCmd.RunSQL "INSERT INTO TABLE1(B,C) VALUES ('something',99)"
Edit: This one also works in spite of supplying a text value for field C. The difference is the text value is one the database engine can convert to a valid number:
DoCmd.RunSQL "INSERT INTO TABLE1(B,C) VALUES ('something','27')"
Is it clearly due to the autonumbering field? Once the record is added (running the query clicking on 'run anyway'), only the autonumber field is blank?
This error shouldn't be happening for Autonumber; at least, I've never seen it before.
It sounds like you are passing in an empty string to one of the values and it's setting that column to NULL.
When you say
Which doesn't prevent it from working
if I click ok
Do all of the fields get populated correctly? Including the Autonumber?