different type of input

31
LICENŢIEREA UTILIZĂRII OPERELOR MUZICALE PE INTERNET PREZENT ŞI PROVOCĂRI BRAŞOV Seminarul naţional privind protecţia juridică a operelor şi a interpretărilor şi execuţiilor în mediu digital 11-12 Decembrie 2012 UCMR-ADA Director General Ana Achim

Upload: fedora

Post on 24-Feb-2016

34 views

Category:

Documents


0 download

DESCRIPTION

Different type of input . Use of Basic language with Excel Silvia Patacchini. Excel offers myriad options for referring to workbooks and sheets in your VBA code. 1: Reference the active workbook - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Different type of input

Different type of input Use of Basic language with

ExcelSilvia Patacchini

Page 2: Different type of input

referring to workbooks and sheets in your VBA code

Excel offers myriad options for referring to workbooks and sheets in your VBA code.

1: Reference the active workbook VBA’s ActiveWorkbook property refers to the workbook

with the focus. The active workbook may or may not contain the code that’s referencing the active workbook, which is an important distinction.

It’s perfectly acceptable to use this property to reference the active workbook from code inside the active workbook. However, it’s invaluable when referencing the active workbook remotely.

Page 3: Different type of input

Sub CloseActiveWBNoSave() 'Close the active workbook without saving. ActiveWorkbook.Close False

End Sub

Sub CloseActiveWBWithSave() 'Close the active workbook and save. ActiveWorkbook.Close True

End Sub

This save takes place without additional information, such as the workbook’s name, path, and so on. However, if you need such information, it’s easy enough to glean using ActiveWorkbook.

An Example

Page 4: Different type of input

Reference the workbook that’s currently running code

VBA’s ThisWorkbook property is similar to the ActiveWorkbook property, but whereas ActiveWorkbook evaluates the workbook with the focus, ThisWorkbook refers to the workbook that’s running the current code.

This added flexibility is great because the active workbook isn’t always the workbook that’s running code.

Page 5: Different type of input

An example

Function GetThisWB() As String GetThisWB = ThisWorkbook.Path & "\" &

ThisWorkbook.Name End Function

Page 6: Different type of input

Reference workbooks in the Workbooks collection

The Workbooks collection contains all the open Workbook objects. Using the Workbooks property, you can refer to open workbooks.

For instance, the following subprocedure populates a list box in a user form with the names of all open workbooks:

Private Sub UserForm_Activate() 'Populate list box with names of open workbooks. Dim wb As Workbook For Each wb In Workbooks

ListBox1.AddItem wb.Name Next wb End Sub

Page 7: Different type of input

Explicitly reference a workbook

If you know the name of the workbook you want to reference, an explicit reference might be the most efficient method.

Although an explicit reference is easy, it does require a stable situation.

If the name of the workbook changes, but the possibilities are known, you can still use an explicit reference by passing the workbook’s name.

For example, the following subprocedure activates an open workbook, as determined by the passed argument, wbname:

Page 8: Different type of input

An Example

Function ActivateWB(wbname As String) 'Open wbname. Workbooks(wbname).Activate

End Function

To execute it, you simply pass the name of the workbook you want to activate as follows:

ActivateWB(“pricing.xlsm")

(You must include the .xlsm extension.)

Page 9: Different type of input

Connection Object

The ADO Connection Object is used to create an open connection to a data source. Through this connection, you can access and manipulate a database.

If you want to access a database multiple times, you should establish a connection using the Connection object. You can also make a connection to a database by passing a connection string via a Command or Recordset object. However, this type of connection is only good for one specific, single query.

Page 10: Different type of input

ADO Programming Code Examples

how to use the ADO library to list and manipulate the objects in Access:

ADO provides only limited ways to manipulate the data structure (typically via DDL query statements), unless you also use the ADOX library which provides the extensions to get to the database catalog.

Page 11: Different type of input

Open a recordset

Function AdoRecordsetExample() Dim rs As New ADODB.Recordset Dim strSql As String

‘** ipotizzo l’esistenza di una connessione aperta al db chiamata‘ CurrentProject.Connection **********

strSql = "SELECT MyField FROM MyTable;" rs.Open strSql, CurrentProject.Connection

rs.Close Set rs = Nothing

End Function

Page 12: Different type of input

.CopyFromRecordset rst

Copies the contents of an ADO or DAO Recordset object onto a worksheet, beginning at the upper-left corner of the specified range. If the Recordset object contains fields with OLE objects in them, this method fails.

This example copies the field names from a DAO Recordset object into the first row of a worksheet and formats the names as bold. The example then copies the recordset onto the worksheet, beginning at cell A2.

For iCols = 0 to rs.Fields.Count - 1 ws.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name Next ws.Range(ws.Cells(1, 1), _ ws.Cells(1, rs.Fields.Count)).Font.Bold = True

ws.Range("A2").CopyFromRecordset rs