简体   繁体   中英

Insert record in access table using VB

I am trying to add a record into an Access table (hyperlinks) and the field "link" should be set to "u:\\directory here\\" I have tried

docmd.runsql "insert into hyperlinks (link."U:\directory here\")" 

and get errors, I tried ' instead of " and I tried without the quotes, all end in error with not specification as to what is wrong.

I would suggest:

docmd.runsql "insert into hyperlinks (link) values ('U:\directory here\')" 

The syntax of an MS Access SQL insert statement can be found here .

If your field is of type Hyperlink (rather than of type text ), your SQL needs to use the following format:

Display Text#Link Path#Sub address#optional screen tip#

If you just want the link path (eg https://google.com ), simply enclose it with the # :

INSERT INTO tHyperlinks (path) VALUES ("#https://google.com#")

More information can be found here

Personally, I don't like to use the Hyperlink data type in Access: I find it's easier to use a simple text field, and generate a hyperlink in a form.

Additionally, I would recommend against using DoCmd.RunSQL . Whenever you use this method, the user will see a confirmation message (allowing them to cancel the update), unless you turn it off: 动作查询确认

To turn off this confirmation, you'd have to something like the following:

DoCmd.SetWarnings False
DoCmd.RunSQL yourSQL
DoCmd.SetWarnings True

Even worse, if the user has changed their options to not show these confirmations by default, you'll show the confirmation (when they've specifically said they don't want to view them).

It is far better to use the Database.Execute method in the DAO library:

Dim db as DAO.Database: Set db = CurrentDB
db.Execute yourSQL, dbFailOnError

The user won't get a confirmation message (regardless of their client settings), and with the dbFailOnError , an error will be generated if the record isn't added, which will give you nice detailed information (the error below is when I deleted a parenthesis):

db.execute错误

You can then perform error handling.

For more information on DoCmd.RunSQL vs. Database.Execute , see these links .

Official Microsoft Docs pages you might find useful:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM