Hi All,
The below code will extract data from Access in to excel sheet with whatever the user inputs the range between the two input box.
But i want whatever the number is enter in column E should return only that.
Any help or guide is highly appreciated.
The below code will extract data from Access in to excel sheet with whatever the user inputs the range between the two input box.
But i want whatever the number is enter in column E should return only that.
Any help or guide is highly appreciated.
Code:
[TABLE="width: 64"]
<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]Option Explicit[/TD]
[/TR]
[TR]
[TD="class: xl63"]'change the path as per suits.[/TD]
[/TR]
[TR]
[TD]Const constraccess As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jalal Kasmani\Desktop\Excel\Excel VBA\Wise Owls & Sujeet\30, 31, 32 connections SQL\Movies.accdb;Persist Security Info=False;"[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Sub copydatafromdatabaselatebinding()[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Dim moviesconn As Object[/TD]
[/TR]
[TR]
[TD]Dim moviesdata As Object[/TD]
[/TR]
[TR]
[TD]Dim moviesfield As Object[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Set moviesconn = CreateObject("adodb.connection")[/TD]
[/TR]
[TR]
[TD]Set moviesdata = CreateObject("adodb.recordset")[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]moviesconn.ConnectionString = constraccess[/TD]
[/TR]
[TR]
[TD]moviesconn.Open[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]On Error GoTo Closeconnection[/TD]
[/TR]
[TR]
[TD]With moviesdata[/TD]
[/TR]
[TR]
[TD] .activeconnection = moviesconn[/TD]
[/TR]
[TR]
[TD] .Source = getsqlstring[/TD]
[/TR]
[TR]
[TD] .locktype = 1[/TD]
[/TR]
[TR]
[TD] .cursortype = 0[/TD]
[/TR]
[TR]
[TD] .Open[/TD]
[/TR]
[TR]
[TD]End With[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Worksheets.Add[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]On Error GoTo Closerecordset[/TD]
[/TR]
[TR]
[TD]For Each moviesfield In moviesdata.Fields[/TD]
[/TR]
[TR]
[TD]ActiveCell.Value = moviesfield.Name[/TD]
[/TR]
[TR]
[TD]ActiveCell.Offset(0, 1).Select[/TD]
[/TR]
[TR]
[TD]Next moviesfield[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Range("a1").Select[/TD]
[/TR]
[TR]
[TD]Range("a2").CopyFromRecordset moviesdata[/TD]
[/TR]
[TR]
[TD]Range("a1").CurrentRegion.EntireColumn.AutoFit[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]On Error GoTo 0[/TD]
[/TR]
[TR]
[TD]Closerecordset:[/TD]
[/TR]
[TR]
[TD]moviesdata.Close[/TD]
[/TR]
[TR]
[TD]Closeconnection:[/TD]
[/TR]
[TR]
[TD]moviesconn.Close[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]End Sub[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Function getsqlstring() As String[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]Dim minlength As Integer, maxlength As Integer[/TD]
[/TR]
[TR]
[TD]Dim SQLstring As String[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]minlength = Application.InputBox("Enter the shortest runtime", Type:=1)[/TD]
[/TR]
[TR]
[TD]maxlength = Application.InputBox("Enter the longest runtime", Type:=1)[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]SQLstring = "SELECT FilmName, FilmReleaseDate, FilmRunTimeMinutes " & _[/TD]
[/TR]
[TR]
[TD] "From tblFilm " & _[/TD]
[/TR]
[TR]
[TD] "WHERE FilmRunTimeMinutes BETWEEN " & minlength & _[/TD]
[/TR]
[TR]
[TD] " And " & maxlength & _[/TD]
[/TR]
[TR]
[TD] " ORDER BY FilmRunTimeMinutes ASC;"[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]getsqlstring = SQLstring[/TD]
[/TR]
[TR]
[TD] [/TD]
[/TR]
[TR]
[TD]End Function[/TD]
[/TR]
</tbody>[/TABLE]