Results 1 to 11 of 11

Thread: Access field names and brackets problem [Resolved!]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Access field names and brackets problem [Resolved!]

    I have an Access db (actually, about 60 of them) that has a table with
    a field named Q#. This table gets read into a dataset. In the dataset,
    the field is also called Q#. But when I try to update the db, an error
    occurs because Access wants the field to be surrounded by brackets -
    [Q#] - when written back. I know this is the problem, because if I
    change the fieldname to QNum, it works fine.
    Using the commandbuilder, or building the INSERT, UPDATE, or DELETE
    manually without brackets, the error is "Syntax error in INSERT
    statement..." (or UPDATE, etc). When I build them manually with
    brackets, the error is "No value given for one or more required
    parameters..."
    An example of the manual statement is: INSERT INTO QS (Q#, QText)
    VALUES(?, ?)
    I don't want to change the fieldname in Access because there are so
    many db's, and the field is used in many queries, forms, etc. I want
    to make all changes to the dataset and update all at once. It works
    using cmd.ExecuteNonQuery, but that's not what I want. Is there a way
    to get this to work? Thanks.
    Last edited by salvelinus; Apr 14th, 2004 at 03:51 PM.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    My two cents:

    Stick with standard names in the DB - if someone created a field named Q#, they probably don't know SQL all that well - best if well designed from the get go.

    But seeing as that's what you have (I've inherited DBs like that also), you could manually wrap all the columns names in square brackets - it won't hurt anything (except your app's performance, I guess).

    Mike

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    I know. The db's were designed several years ago by someone with no experience in Access (she also used lots of macros, which are a pain to go through and debug). I've already told everyone else (that's two other people) not to use names like that, or more than one word for table names. Eventually we'll be converting to mySql, so I can design that better, only one or two db's as well.

    Anyway, would surrounding all field names with brackets make a difference? Surrounding the problem names with brackets doesn't fix it.

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Anyway, would surrounding all field names with brackets make a difference? Surrounding the problem names with brackets doesn't fix it.
    Oh, sorry, I guess I read your original post as the brackets did fix it. I don't use Access. Does it not like square brackets? Would quotes work?

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Access is weird. It allows Q# as a field name, but because of the #, code has to surround the field name with brackets to run queries on the db. Same if you use two words; SomeField is ok, but Some Field has to be [Some Field] in code. Reading it out of the db as Q# is ok.
    I think the problem is that [Q#] doesn't exist in the dataset; Q# does. So adding brackets manually throws the dataset off; not adding them throws Access off. This would explain why using ExecuteNonQuery with brackets works - no dataset involved.
    This is the basic problem, unless I'm on the wrong track.

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    That's making sense now. How are you populating your dataset? Can you use an AS statement? e.g. SELECT Q# AS QNum FROM MyTable.

    I use MS SQL 2000, so not too sure about Access.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Interesting idea. I'l try it out and let you know.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Unfortunately, couldn't get that to work either, probably because the dataset has QNum while the table has Q#. The datatable in the dataset has the actual field name as QNum.
    I'd have to do some kind of AS statement when writing back to the db, which I don't think is possible.
    For the time I've spent on this (parts of five days), I probably could've converted the names in all the db's. But dammit, this ought to be a simple procedure.

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    I have no idea. Not sure if it will help, but using sql server this works fine:

    VB Code:
    1. Dim conn As SqlConnection = New SqlConnection
    2.         conn.ConnectionString = "Persist Security Info=False;" & _
    3.                         "Integrated Security=SSPI;" & _
    4.                         "database=Dispatch;" & _
    5.                         "server=127.0.0.1"
    6.  
    7.         Dim ds As New DataSet("MyData")
    8.         Dim da As New SqlDataAdapter
    9.         da.SelectCommand = New SqlCommand("select q# from mytable", conn)
    10.         da.Fill(ds)
    11.         da.UpdateCommand = New SqlCommand("update mytable set q# = 'updated from vb.net'", conn)
    12.         Dim dr As DataRow = ds.Tables(0).Rows(0)
    13.         dr("q#") = "dummy string"
    14.         da.Update(ds)

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Yeah, I think it's an Access issue. Thanks for trying. I thought about using datarows, but who knows how many changes the user will make? I suppose it could be done, but it ought to be simple. It's all MS, after all, not like trying to get it to work with AS/400.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Got it!

    Got it! Have to set the QuotePrefix and QuoteSuffix properties of the commandbuilder to [ and ]. Man, that's a relief. Haven't seen those mentioned in any forum, google group or book anywhere.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width