View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dick Kusleika[_4_] Dick Kusleika[_4_] is offline
external usenet poster
 
Posts: 595
Default AutoIncrementing a Column like a database in Excel 2007

On Thu, 24 Jul 2008 15:21:01 -0700, keith
wrote:

We're going to use an SQLite database but we'd like to do all our data
entry in excel. Then we'd like to save the sheets as .csv files and import
the data into an SQLite database. For us to do this successfully we need to
be able to AutoIncrement an id field in Excel like a database would. Does
anyone know the best way to do this?


So you need to get the next available unique number from the SQLite
database? This example uses Access, so you'll have to change the connection
string to use the SQLite driver.

Public Function GetNextIndex()

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sCn As String, sSql As String

sCn = "DSN=MS Access Database;DBQ=C:\service\service.mdb;"
sSql = "SELECT MAX(index) FROM Parts_Used"

Set cn = New ADODB.Connection
cn.Open sCn
Set rs = cn.Execute(sSql)

If Not (rs.BOF And rs.EOF) Then
GetNextIndex = rs.Fields(0).Value + 1
Else
GetNextIndex = 1
End If

End Function
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com