What is the MS Access SQL syntax to create a field of type Hyperlink?

谁说我不能喝 提交于 2020-01-21 05:25:26

问题


I'm working on a C# project that uses System.Data.OleDb.OleDbCommand class to create and alter tables in an MS Access DB. I generate the SQL statement, pass it to the object, then call the ExecuteNonQuery function. I was able to figure out the proper MS Access SQL syntax to create columns of the following Access data types:

AutoNumber: ALTER TABLE table-name ADD COLUMN column-name COUNTER|AUTOINCREMENT
Currency:   ALTER TABLE table-name ADD COLUMN column-name MONEY
Date/Time:  ALTER TABLE table-name ADD COLUMN column-name DATE
Memo:       ALTER TABLE table-name ADD COLUMN column-name MEMO|TEXT
Number:     ALTER TABLE table-name ADD COLUMN column-name NUMBER
OLE Object: ALTER TABLE table-name ADD COLUMN column-name OLEOBJECT
Text:       ALTER TABLE table-name ADD COLUMN column-name CHARACTER
Yes/No:     ALTER TABLE table-name ADD COLUMN column-name BIT

The only type I haven't figured out how to create is Hyperlink. Does anyone know the syntax for that one?


回答1:


There is no real concept in sql of a datatype of hyperlink. You would store the url as you would any text and then when you're using it in your application you would create the hyperlink on that end.




回答2:


This is a case where you can't use DDL but must use DAO. What you're looking for is the .Attributes property of the DAO Field type. In VBA, this code does the trick (you'll have to figure out how to do this with DAO in C#):

  Dim tdf As DAO.TableDef
  Dim fld As DAO.Field

  Set tdf = CurrentDB.TableDefs("MyTable")
  Set fld = tdf.CreateField("Hyperlink", dbMemo) ' dbMemo = 12
  fld.Attributes = dbHyperlinkField ' 32768
  tdf.Fields.Append fld
  Set fld = Nothing
  Set tdf = Nothing

If you check the data type of this field in Access:

  CurrentDB.TableDefs("MyTable").Fields("Hyperlink").Type

it returns 12, which is equal to the VBA global constant dbMemo (that's how I figured out how to do it, i.e., created a Hyperlink field in the Access UI, then checked its datatype). The value for dbHyperlinkField is 32768, but cannot be assigned directly as a data type -- it is instead a sub-attribute of a memo field.

This is a case of Access extending Jet by using a custom attribute for it to work with the data differently than the limited number of Jet data types. It's also clear that the Hyperlink field type is only relevant if you're using the data from Access itself. If you're using some other application to work with the data, you're gaining nothing at all from using a hyperlink data field.

Also, I'm with those who recommend not using a field of type Hyperlink because I've tried it and it's just a pain in the ass. Also, given that it's really a memo field, it's subject to all the problems that come with memo fields. That is, the data is not actually stored with the data table -- all that's stored in the main record is a pointer to the data page where the real data is stored, and those pointers are one of the frailer parts of Jet. Memos are worth that risk, because when you need to store more than 255 characters, you need to do it. I can't see that there's any significant functionality added by the Access-specific Hyperlink field type that it's worth the trouble in working with it in code or the risk that comes from the fact that it's implemented behind the scenes as a memo field.




回答3:


I had a play around with this with ADOX in C# and the only way I found to do it was set the "Jet OLEDB:Hyperlink" property after creating the column in the table but before actually adding the table to the Tables collection.

The following example requires a COM reference to Microsoft ADO Ext. 2.8 for DDL and Security:

ADOX.Catalog cat = new CatalogClass();    
cat.let_ActiveConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;" + 
    "Data Source=C:\\Tempo\\Test_Access2007.accdb");

ADOX.Table tbl = new ADOX.TableClass();
tbl.Name = "TestHyperlink";

tbl.Columns.Append("my_hyperlink", ADOX.DataTypeEnum.adLongVarWChar, 0);

ADOX.Column col = tbl.Columns["my_hyperlink"];

col.ParentCatalog = cat;
col.Properties["Jet OLEDB:Hyperlink"].Value = true;

cat.Tables.Append(tbl);



回答4:


Although Microsoft complicates matters by having a hyperlink field in a database as an object with properties, setting it seems reasonably straightforward, using this kind of syntax :-

UPDATE myTable SET HyperlinkField = "(text to display)#(Target URL)#(hyperlink subfield, if required)" WHERE clause

Actually, I'm using this to create a form over a table, where the link points to a file rather than a web address (it's a list of drawings so the user can click the link to open the drawing). In this case, the URL is the RELATIVE path to the file, e.g. "..\documents\myFile1.dat".

I'm aware I could use other methods to implement the same, but the users understand hyperlinks.

I've not tested how these links behave when the source database is moved, incidentally.




回答5:


You would use two CHARACTER fields to represent the content of a hyperlink, one that contains the URL to the resource you want the hyperlink to link to (stored as plain text), and another that would hold the text that's shown in the browser (that is, if the text shown in the browser is different from the URL itself).

When displaying the link in a webpage in C#, you can create a hyperlink:

Hyperlink hyp1 = new Hyperlink
hyp1.Text = datarow.DisplayText
hyp1.NavigateURL = datarow.TargetURL



回答6:


Following raven instructions, dummy programmers (like me) can use this sub to add, by access VBA code, an hyperlink field.

Sub AddLinkField(Table, FieldName)
    Dim dbs As DAO.Database
    Set dbs = CurrentDb()
    Dim tdf As DAO.TableDef
    Dim fld As DAO.Field
    Set tdf = dbs.TableDefs(Table)
    Set fld = tdf.CreateField(FieldName, dbMemo) ' dbMemo = 12
    fld.Attributes = dbHyperlinkField ' 32768
    tdf.Fields.Append fld
    Set fld = Nothing
    Set tdf = Nothing
End Sub

The relevant SQL to insert a new record with a link to (e.g) c:\ is..

SQL = "INSERT INTO Table VALUES (...,'LinkVisualizedText #c:\#')"

to execute it is...

CurrentDB.Execute SQL


来源:https://stackoverflow.com/questions/1133523/what-is-the-ms-access-sql-syntax-to-create-a-field-of-type-hyperlink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!