Assistance Pretty Please

  • Thread starter Thread starter Jayyde
  • Start date Start date
J

Jayyde

Need the help of all you gurus out there again. Alright, what I have going
this time is: 1 form that allows the user to select product type and which
fields they want included for an export to excel. Great, I haven't actually
tested the export yet, but I'm not there yet either. On this form I have a
subform which is obviously not actually linked to anything and is there to
be a preview pane of sorts for the upcoming export. It's this subform that
is currently being a pain in my posterier. I've tried a few different ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've checked
the SQL statement itself so I know that's good, incredibly long potentially
but syntatically Ok. No matter which way I've tried this I get errors, If
anyone knows an easier way to do this feel super-free to let me know what it
is =).

Thanx ahead of time!
-Jayyde
 
First, be aware that there's a difference between a subform control and a
form being used as a subform. You've got a subform control on the main form.
One of the properties of that subform control is the Source Object, which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the subform
control is (usually) named the same as the form being used as a subform.
However, it may end up with a different name. If you create your subform by
adding a subform control from the toolbox onto the main form, the name of
the subform control will definitely not be the same as the form being used
as a subform.

What you need to use in your code is the name of the subform control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL
 
This is not correct:
[subform].form.recordsource = [theSQLstatement]
This might work
Forms!MainFormName!SubFormName.RecordSource = TheSQL
or, if your code is in the main form:
Me!SubFormName.RecordSource = TheSQL
 
There is no me.[subformname].recordsource... Only sourceobject

Klatuu said:
This is not correct:
[subform].form.recordsource = [theSQLstatement]
This might work
Forms!MainFormName!SubFormName.RecordSource = TheSQL
or, if your code is in the main form:
Me!SubFormName.RecordSource = TheSQL

Jayyde said:
Need the help of all you gurus out there again. Alright, what I have
going
this time is: 1 form that allows the user to select product type and
which
fields they want included for an export to excel. Great, I haven't
actually
tested the export yet, but I'm not there yet either. On this form I have
a
subform which is obviously not actually linked to anything and is there
to
be a preview pane of sorts for the upcoming export. It's this subform
that
is currently being a pain in my posterier. I've tried a few different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get errors,
If
anyone knows an easier way to do this feel super-free to let me know what
it
is =).

Thanx ahead of time!
-Jayyde
 
That's basically what I'm doing. It's been renamed. The literal code I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here Doug, I'm
using your Excel export stuff as a base (trying anyway). What in that code
would make it create the recordset, let it pass through the .BOF & .EOF =
false statement then throw errors on the fields of "Object variable or With
block variable not set"?

Thanx guys! =)
-Jayyde


Douglas J Steele said:
First, be aware that there's a difference between a subform control and a
form being used as a subform. You've got a subform control on the main
form.
One of the properties of that subform control is the Source Object, which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a subform.
However, it may end up with a different name. If you create your subform
by
adding a subform control from the toolbox onto the main form, the name of
the subform control will definitely not be the same as the form being used
as a subform.

What you need to use in your code is the name of the subform control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jayyde said:
Need the help of all you gurus out there again. Alright, what I have going
this time is: 1 form that allows the user to select product type and
which
fields they want included for an export to excel. Great, I haven't actually
tested the export yet, but I'm not there yet either. On this form I have a
subform which is obviously not actually linked to anything and is there
to
be a preview pane of sorts for the upcoming export. It's this subform that
is currently being a pain in my posterier. I've tried a few different ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've checked
the SQL statement itself so I know that's good, incredibly long potentially
but syntatically Ok. No matter which way I've tried this I get errors,
If
anyone knows an easier way to do this feel super-free to let me know what it
is =).

Thanx ahead of time!
-Jayyde
 
Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jayyde said:
That's basically what I'm doing. It's been renamed. The literal code I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here Doug, I'm
using your Excel export stuff as a base (trying anyway). What in that code
would make it create the recordset, let it pass through the .BOF & .EOF =
false statement then throw errors on the fields of "Object variable or With
block variable not set"?

Thanx guys! =)
-Jayyde


Douglas J Steele said:
First, be aware that there's a difference between a subform control and a
form being used as a subform. You've got a subform control on the main
form.
One of the properties of that subform control is the Source Object, which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a subform.
However, it may end up with a different name. If you create your subform
by
adding a subform control from the toolbox onto the main form, the name of
the subform control will definitely not be the same as the form being used
as a subform.

What you need to use in your code is the name of the subform control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jayyde said:
Need the help of all you gurus out there again. Alright, what I have going
this time is: 1 form that allows the user to select product type and
which
fields they want included for an export to excel. Great, I haven't actually
tested the export yet, but I'm not there yet either. On this form I
have
a
subform which is obviously not actually linked to anything and is there
to
be a preview pane of sorts for the upcoming export. It's this subform that
is currently being a pain in my posterier. I've tried a few different ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've checked
the SQL statement itself so I know that's good, incredibly long potentially
but syntatically Ok. No matter which way I've tried this I get errors,
If
anyone knows an easier way to do this feel super-free to let me know
what
it
is =).

Thanx ahead of time!
-Jayyde
 
lol. As far as the subform goes put the me. in there and still get the
error:
"The record source 'SELECT blah blah...' specified on this form or
report does not exist"

The "export stuff" was a pdf that you had on smart access or somesuch. The
code I'm trying is:


*******************CODE**
Dim oActiveWorkbook As Object
Dim oXL As Object
Dim oWorksheet As Object
Dim rs As Recordset
Dim fld As Field
Dim blnXLCreated As Boolean
Dim intCurrentColumn As Integer
Dim intCurrentSheet As Integer
Dim strMsg As String
Dim strSQL As String
Dim strWorkbookName As String
Dim strSelectSQL As String
Dim strQuery As String

strWorkbookName = CurrentDb().name
strWorkbookName = Left$(strWorkbookName, InStr(strWorkbookName,
".mdb") - 1) & " - " & Format(VBA.Date(), "dd-mmm-yyyy") & ".xls"

If Len(Dir(strWorkbookName)) > 0 Then
strMsg = strWorkbookName & " already exists." & vbCrLf & "Delete
it?"

Select Case MsgBox(strMsg, vbYesNoCancel + vbQuestion, "Overwrite
Verification")
Case vbYes
Kill strWorkbookName
Case vbCancel
Exit Sub
Case Else
End Select
End If

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err.Number = 0 Then
blnXLCreated = False
Else
Set oXL = CreateObject("Excel.Application")
blnXLCreated = True
End If

'On Error GoTo Err_ExportToExcel

oXL.Application.Workbooks.Add
Set oActiveWorkbook = oXL.Application.ActiveWorkbook

strSelectSQL = GetSelectSQL
strQuery = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set rs = CurrentDb.OpenRecordset(strSQL)

If (rs.BOF = False And rs.EOF = False) Then
intCurrentSheet = intCurrentSheet + 1

If (oActiveWorkbook.Worksheets.Count < intCurrentSheet) Then
oActiveWorkbook.Worksheets.Add
After:=oActiveWorkbook.Worksheets(intCurrentSheet - 1)
End If

With oActiveWorkbook.Worksheets(intCurrentSheet)
.name = strQuery

intCurrentColumn = 1

For Each fld In rs.Fields
.Cells(1, intCurrentColumn) = fld.name
intCurrentColumn = intCurrentColumn + 1
Next fld

.Cells(2, 1).CopyFromRecordset rs
.Rows("1:1").Font.Bold = True
.Columns.Autofit
End With

End If

rs.Close

oActiveWorkbook.Close SaveChanges:=True, FileName:=strWorkbookName

Set rs = Nothing
Set db = Nothing
Set oActiveWorkbook = Nothing

If blnXLCreated Then
oXL.Application.Quit
End If

Set oXL = Nothing
********************CODE**

And like I said, when it gets to the rs.fields it says something about
object or with block not set. Just figured I'd pick your brain while you
were here ;).

-Jayyde

Douglas J Steele said:
Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jayyde said:
That's basically what I'm doing. It's been renamed. The literal code I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here Doug, I'm
using your Excel export stuff as a base (trying anyway). What in that code
would make it create the recordset, let it pass through the .BOF & .EOF =
false statement then throw errors on the fields of "Object variable or With
block variable not set"?

Thanx guys! =)
-Jayyde


Douglas J Steele said:
First, be aware that there's a difference between a subform control and a
form being used as a subform. You've got a subform control on the main
form.
One of the properties of that subform control is the Source Object, which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a
subform.
However, it may end up with a different name. If you create your
subform
by
adding a subform control from the toolbox onto the main form, the name of
the subform control will definitely not be the same as the form being used
as a subform.

What you need to use in your code is the name of the subform control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Need the help of all you gurus out there again. Alright, what I have
going
this time is: 1 form that allows the user to select product type and
which
fields they want included for an export to excel. Great, I haven't
actually
tested the export yet, but I'm not there yet either. On this form I have
a
subform which is obviously not actually linked to anything and is
there
to
be a preview pane of sorts for the upcoming export. It's this subform
that
is currently being a pain in my posterier. I've tried a few different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get
errors,
If
anyone knows an easier way to do this feel super-free to let me know what
it
is =).

Thanx ahead of time!
-Jayyde
 
**********************CODE**
Private Sub btnPreview_Click()

Dim strSQL As String
Dim strSelectSQL As String
Dim strQuery As String
Dim db As Database
Dim qdf As QueryDef
Dim strPreviewName As String
Dim strCrap As String

strPreviewName = "qryExportPreview"
strSelectSQL = GetSelectSQL
strCrap = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set db = CurrentDb
Set qdf = db.CreateQueryDef("qryPreview", strSQL)

Me.subExportPreview.Form.RecordSource = "qryPreview"
subExportPreview.Requery

db.QueryDefs.Delete ("qryPreview")
Set db = Nothing

End Sub

Private Function GetSelectSQL() As String

Dim i As Integer
Dim j As Integer
Dim strSQL As String

With lstPMIncluded
If (.RowSource <> "") Then
strSQL = "SELECT "

For i = 0 To .ListCount - 1
strSQL = strSQL & .Column(0, i) & " AS [" & .Column(1, i) & "],
"
Next i
End If
End With

With lstPIncluded
If (.RowSource <> "") Then
If (strSQL = "") Then
strSQL = "SELECT "
End If

For i = 0 To .ListCount - 1
strSQL = strSQL & .Column(0, i) & " AS [" & .Column(1, i) & "],
"
Next i
End If
End With

strSQL = Left(strSQL, Len(Trim(strSQL)) - 1)

GetSelectSQL = strSQL

End Function

Private Function GetQuery() As String

strQuerySQL = ""
strQuerySQL = " FROM dbo_vwProductMasterView AS PM LEFT JOIN
dbo_vwProductView AS P " _
& "ON PM.iProductMasterId=P.iProductMasterId"

If (fraProdType = 1) Then
strQuerySQL = strQuerySQL & " WHERE sProductType='BEER'"
GetQuery = "Beer"
ElseIf (fraProdType = 2) Then
strQuerySQL = strQuerySQL & " WHERE sProductType='WINE'"
GetQuery = "Wine"
Else
strQuerySQL = strQuerySQL & " WHERE sProductType='SPIRIT'"
GetQuery = "Spirit"
End If

End Function
**********************CODE**

I'm getting desperate. This code has it to the point where it's returning
the right number of records (6kish), but not displaying any of the columns.
If I grab the SQL for the query def and plug it into query analizer it's
lovin' it. Is Access doing something weird here? or am I losing my mind?

-Jayyde



Jayyde said:
Need the help of all you gurus out there again. Alright, what I have
going this time is: 1 form that allows the user to select product type and
which fields they want included for an export to excel. Great, I haven't
actually tested the export yet, but I'm not there yet either. On this
form I have a subform which is obviously not actually linked to anything
and is there to be a preview pane of sorts for the upcoming export. It's
this subform that is currently being a pain in my posterier. I've tried a
few different ways so far. I've tried creating a queryDef then setting
that as [subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've
checked the SQL statement itself so I know that's good, incredibly long
potentially but syntatically Ok. No matter which way I've tried this I
get errors, If anyone knows an easier way to do this feel super-free to
let me know what it is =).

Thanx ahead of time!
-Jayyde
 
So when you write the content of strSQL to the immediate window, copy it
into a query and run it, it runs correctly?

Do you know exactly what line of the Excel code it's complaining about?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Jayyde said:
lol. As far as the subform goes put the me. in there and still get the
error:
"The record source 'SELECT blah blah...' specified on this form or
report does not exist"

The "export stuff" was a pdf that you had on smart access or somesuch.
The code I'm trying is:


*******************CODE**
Dim oActiveWorkbook As Object
Dim oXL As Object
Dim oWorksheet As Object
Dim rs As Recordset
Dim fld As Field
Dim blnXLCreated As Boolean
Dim intCurrentColumn As Integer
Dim intCurrentSheet As Integer
Dim strMsg As String
Dim strSQL As String
Dim strWorkbookName As String
Dim strSelectSQL As String
Dim strQuery As String

strWorkbookName = CurrentDb().name
strWorkbookName = Left$(strWorkbookName, InStr(strWorkbookName,
".mdb") - 1) & " - " & Format(VBA.Date(), "dd-mmm-yyyy") & ".xls"

If Len(Dir(strWorkbookName)) > 0 Then
strMsg = strWorkbookName & " already exists." & vbCrLf & "Delete
it?"

Select Case MsgBox(strMsg, vbYesNoCancel + vbQuestion, "Overwrite
Verification")
Case vbYes
Kill strWorkbookName
Case vbCancel
Exit Sub
Case Else
End Select
End If

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err.Number = 0 Then
blnXLCreated = False
Else
Set oXL = CreateObject("Excel.Application")
blnXLCreated = True
End If

'On Error GoTo Err_ExportToExcel

oXL.Application.Workbooks.Add
Set oActiveWorkbook = oXL.Application.ActiveWorkbook

strSelectSQL = GetSelectSQL
strQuery = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set rs = CurrentDb.OpenRecordset(strSQL)

If (rs.BOF = False And rs.EOF = False) Then
intCurrentSheet = intCurrentSheet + 1

If (oActiveWorkbook.Worksheets.Count < intCurrentSheet) Then
oActiveWorkbook.Worksheets.Add
After:=oActiveWorkbook.Worksheets(intCurrentSheet - 1)
End If

With oActiveWorkbook.Worksheets(intCurrentSheet)
.name = strQuery

intCurrentColumn = 1

For Each fld In rs.Fields
.Cells(1, intCurrentColumn) = fld.name
intCurrentColumn = intCurrentColumn + 1
Next fld

.Cells(2, 1).CopyFromRecordset rs
.Rows("1:1").Font.Bold = True
.Columns.Autofit
End With

End If

rs.Close

oActiveWorkbook.Close SaveChanges:=True, FileName:=strWorkbookName

Set rs = Nothing
Set db = Nothing
Set oActiveWorkbook = Nothing

If blnXLCreated Then
oXL.Application.Quit
End If

Set oXL = Nothing
********************CODE**

And like I said, when it gets to the rs.fields it says something about
object or with block not set. Just figured I'd pick your brain while you
were here ;).

-Jayyde

Douglas J Steele said:
Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jayyde said:
That's basically what I'm doing. It's been renamed. The literal code I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here Doug, I'm
using your Excel export stuff as a base (trying anyway). What in that code
would make it create the recordset, let it pass through the .BOF & .EOF
=
false statement then throw errors on the fields of "Object variable or With
block variable not set"?

Thanx guys! =)
-Jayyde


First, be aware that there's a difference between a subform control
and a
form being used as a subform. You've got a subform control on the main
form.
One of the properties of that subform control is the Source Object, which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a
subform.
However, it may end up with a different name. If you create your
subform
by
adding a subform control from the toolbox onto the main form, the name of
the subform control will definitely not be the same as the form being used
as a subform.

What you need to use in your code is the name of the subform control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Need the help of all you gurus out there again. Alright, what I have
going
this time is: 1 form that allows the user to select product type and
which
fields they want included for an export to excel. Great, I haven't
actually
tested the export yet, but I'm not there yet either. On this form I have
a
subform which is obviously not actually linked to anything and is
there
to
be a preview pane of sorts for the upcoming export. It's this
subform
that
is currently being a pain in my posterier. I've tried a few
different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get
errors,
If
anyone knows an easier way to do this feel super-free to let me know what
it
is =).

Thanx ahead of time!
-Jayyde
 
Yeah, the SQL's fine. I stopped it in there just to make sure, but it's
also the same SQL for that preview pane (which is working now thank God).
It's erroring out on the:

.Cells(1, intCurrentColumn) = fld.name

line.

Thanx, Doug
-Jayyde

p.s. Leaving work for weekend now, so I'll keep checking back but won't have
the code to test in front of me =\

Douglas J. Steele said:
So when you write the content of strSQL to the immediate window, copy it
into a query and run it, it runs correctly?

Do you know exactly what line of the Excel code it's complaining about?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Jayyde said:
lol. As far as the subform goes put the me. in there and still get the
error:
"The record source 'SELECT blah blah...' specified on this form or
report does not exist"

The "export stuff" was a pdf that you had on smart access or somesuch.
The code I'm trying is:


*******************CODE**
Dim oActiveWorkbook As Object
Dim oXL As Object
Dim oWorksheet As Object
Dim rs As Recordset
Dim fld As Field
Dim blnXLCreated As Boolean
Dim intCurrentColumn As Integer
Dim intCurrentSheet As Integer
Dim strMsg As String
Dim strSQL As String
Dim strWorkbookName As String
Dim strSelectSQL As String
Dim strQuery As String

strWorkbookName = CurrentDb().name
strWorkbookName = Left$(strWorkbookName, InStr(strWorkbookName,
".mdb") - 1) & " - " & Format(VBA.Date(), "dd-mmm-yyyy") & ".xls"

If Len(Dir(strWorkbookName)) > 0 Then
strMsg = strWorkbookName & " already exists." & vbCrLf & "Delete
it?"

Select Case MsgBox(strMsg, vbYesNoCancel + vbQuestion, "Overwrite
Verification")
Case vbYes
Kill strWorkbookName
Case vbCancel
Exit Sub
Case Else
End Select
End If

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err.Number = 0 Then
blnXLCreated = False
Else
Set oXL = CreateObject("Excel.Application")
blnXLCreated = True
End If

'On Error GoTo Err_ExportToExcel

oXL.Application.Workbooks.Add
Set oActiveWorkbook = oXL.Application.ActiveWorkbook

strSelectSQL = GetSelectSQL
strQuery = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set rs = CurrentDb.OpenRecordset(strSQL)

If (rs.BOF = False And rs.EOF = False) Then
intCurrentSheet = intCurrentSheet + 1

If (oActiveWorkbook.Worksheets.Count < intCurrentSheet) Then
oActiveWorkbook.Worksheets.Add
After:=oActiveWorkbook.Worksheets(intCurrentSheet - 1)
End If

With oActiveWorkbook.Worksheets(intCurrentSheet)
.name = strQuery

intCurrentColumn = 1

For Each fld In rs.Fields
.Cells(1, intCurrentColumn) = fld.name
intCurrentColumn = intCurrentColumn + 1
Next fld

.Cells(2, 1).CopyFromRecordset rs
.Rows("1:1").Font.Bold = True
.Columns.Autofit
End With

End If

rs.Close

oActiveWorkbook.Close SaveChanges:=True, FileName:=strWorkbookName

Set rs = Nothing
Set db = Nothing
Set oActiveWorkbook = Nothing

If blnXLCreated Then
oXL.Application.Quit
End If

Set oXL = Nothing
********************CODE**

And like I said, when it gets to the rs.fields it says something about
object or with block not set. Just figured I'd pick your brain while you
were here ;).

-Jayyde

Douglas J Steele said:
Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


That's basically what I'm doing. It's been renamed. The literal code
I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here Doug,
I'm
using your Excel export stuff as a base (trying anyway). What in that
code
would make it create the recordset, let it pass through the .BOF & .EOF
=
false statement then throw errors on the fields of "Object variable or
With
block variable not set"?

Thanx guys! =)
-Jayyde


First, be aware that there's a difference between a subform control
and
a
form being used as a subform. You've got a subform control on the
main
form.
One of the properties of that subform control is the Source Object,
which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a
subform.
However, it may end up with a different name. If you create your
subform
by
adding a subform control from the toolbox onto the main form, the
name
of
the subform control will definitely not be the same as the form being
used
as a subform.

What you need to use in your code is the name of the subform control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Need the help of all you gurus out there again. Alright, what I
have
going
this time is: 1 form that allows the user to select product type and
which
fields they want included for an export to excel. Great, I haven't
actually
tested the export yet, but I'm not there yet either. On this form I
have
a
subform which is obviously not actually linked to anything and is
there
to
be a preview pane of sorts for the upcoming export. It's this
subform
that
is currently being a pain in my posterier. I've tried a few
different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go. I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get
errors,
If
anyone knows an easier way to do this feel super-free to let me know
what
it
is =).

Thanx ahead of time!
-Jayyde
 
I'm wondering whether it's because it's DAO code, and you haven't explicitly
declared the objects as DAO.

See whether

Dim rs As DAO.Recordset
Dim fld As DAO.Field

works any better.

If you reread the column (July, 2005, available at
http://www.accessmvp.com/DJSteele/SmartAccess.html), I think you'll find I
was explicit in the sample code.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Jayyde said:
Yeah, the SQL's fine. I stopped it in there just to make sure, but it's
also the same SQL for that preview pane (which is working now thank God).
It's erroring out on the:

.Cells(1, intCurrentColumn) = fld.name

line.

Thanx, Doug
-Jayyde

p.s. Leaving work for weekend now, so I'll keep checking back but won't
have the code to test in front of me =\

Douglas J. Steele said:
So when you write the content of strSQL to the immediate window, copy it
into a query and run it, it runs correctly?

Do you know exactly what line of the Excel code it's complaining about?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Jayyde said:
lol. As far as the subform goes put the me. in there and still get the
error:
"The record source 'SELECT blah blah...' specified on this form or
report does not exist"

The "export stuff" was a pdf that you had on smart access or somesuch.
The code I'm trying is:


*******************CODE**
Dim oActiveWorkbook As Object
Dim oXL As Object
Dim oWorksheet As Object
Dim rs As Recordset
Dim fld As Field
Dim blnXLCreated As Boolean
Dim intCurrentColumn As Integer
Dim intCurrentSheet As Integer
Dim strMsg As String
Dim strSQL As String
Dim strWorkbookName As String
Dim strSelectSQL As String
Dim strQuery As String

strWorkbookName = CurrentDb().name
strWorkbookName = Left$(strWorkbookName, InStr(strWorkbookName,
".mdb") - 1) & " - " & Format(VBA.Date(), "dd-mmm-yyyy") & ".xls"

If Len(Dir(strWorkbookName)) > 0 Then
strMsg = strWorkbookName & " already exists." & vbCrLf & "Delete
it?"

Select Case MsgBox(strMsg, vbYesNoCancel + vbQuestion, "Overwrite
Verification")
Case vbYes
Kill strWorkbookName
Case vbCancel
Exit Sub
Case Else
End Select
End If

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err.Number = 0 Then
blnXLCreated = False
Else
Set oXL = CreateObject("Excel.Application")
blnXLCreated = True
End If

'On Error GoTo Err_ExportToExcel

oXL.Application.Workbooks.Add
Set oActiveWorkbook = oXL.Application.ActiveWorkbook

strSelectSQL = GetSelectSQL
strQuery = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set rs = CurrentDb.OpenRecordset(strSQL)

If (rs.BOF = False And rs.EOF = False) Then
intCurrentSheet = intCurrentSheet + 1

If (oActiveWorkbook.Worksheets.Count < intCurrentSheet) Then
oActiveWorkbook.Worksheets.Add
After:=oActiveWorkbook.Worksheets(intCurrentSheet - 1)
End If

With oActiveWorkbook.Worksheets(intCurrentSheet)
.name = strQuery

intCurrentColumn = 1

For Each fld In rs.Fields
.Cells(1, intCurrentColumn) = fld.name
intCurrentColumn = intCurrentColumn + 1
Next fld

.Cells(2, 1).CopyFromRecordset rs
.Rows("1:1").Font.Bold = True
.Columns.Autofit
End With

End If

rs.Close

oActiveWorkbook.Close SaveChanges:=True, FileName:=strWorkbookName

Set rs = Nothing
Set db = Nothing
Set oActiveWorkbook = Nothing

If blnXLCreated Then
oXL.Application.Quit
End If

Set oXL = Nothing
********************CODE**

And like I said, when it gets to the rs.fields it says something about
object or with block not set. Just figured I'd pick your brain while
you were here ;).

-Jayyde

Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


That's basically what I'm doing. It's been renamed. The literal code
I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here
Doug,
I'm
using your Excel export stuff as a base (trying anyway). What in that
code
would make it create the recordset, let it pass through the .BOF &
.EOF =
false statement then throw errors on the fields of "Object variable or
With
block variable not set"?

Thanx guys! =)
-Jayyde


message
First, be aware that there's a difference between a subform control
and
a
form being used as a subform. You've got a subform control on the
main
form.
One of the properties of that subform control is the Source Object,
which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a
subform.
However, it may end up with a different name. If you create your
subform
by
adding a subform control from the toolbox onto the main form, the
name
of
the subform control will definitely not be the same as the form
being
used
as a subform.

What you need to use in your code is the name of the subform
control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Need the help of all you gurus out there again. Alright, what I
have
going
this time is: 1 form that allows the user to select product type
and
which
fields they want included for an export to excel. Great, I haven't
actually
tested the export yet, but I'm not there yet either. On this form
I
have
a
subform which is obviously not actually linked to anything and is
there
to
be a preview pane of sorts for the upcoming export. It's this
subform
that
is currently being a pain in my posterier. I've tried a few
different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go.
I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get
errors,
If
anyone knows an easier way to do this feel super-free to let me
know
what
it
is =).

Thanx ahead of time!
-Jayyde
 
I tried it that way initially and only took that out because I wondered if
ADODB. would work any better, then tried it without explicitly declaring it
either way. None of them worked =\.

Douglas J. Steele said:
I'm wondering whether it's because it's DAO code, and you haven't
explicitly declared the objects as DAO.

See whether

Dim rs As DAO.Recordset
Dim fld As DAO.Field

works any better.

If you reread the column (July, 2005, available at
http://www.accessmvp.com/DJSteele/SmartAccess.html), I think you'll find I
was explicit in the sample code.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Jayyde said:
Yeah, the SQL's fine. I stopped it in there just to make sure, but it's
also the same SQL for that preview pane (which is working now thank God).
It's erroring out on the:

.Cells(1, intCurrentColumn) = fld.name

line.

Thanx, Doug
-Jayyde

p.s. Leaving work for weekend now, so I'll keep checking back but won't
have the code to test in front of me =\

Douglas J. Steele said:
So when you write the content of strSQL to the immediate window, copy it
into a query and run it, it runs correctly?

Do you know exactly what line of the Excel code it's complaining about?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


lol. As far as the subform goes put the me. in there and still get the
error:
"The record source 'SELECT blah blah...' specified on this form or
report does not exist"

The "export stuff" was a pdf that you had on smart access or somesuch.
The code I'm trying is:


*******************CODE**
Dim oActiveWorkbook As Object
Dim oXL As Object
Dim oWorksheet As Object
Dim rs As Recordset
Dim fld As Field
Dim blnXLCreated As Boolean
Dim intCurrentColumn As Integer
Dim intCurrentSheet As Integer
Dim strMsg As String
Dim strSQL As String
Dim strWorkbookName As String
Dim strSelectSQL As String
Dim strQuery As String

strWorkbookName = CurrentDb().name
strWorkbookName = Left$(strWorkbookName, InStr(strWorkbookName,
".mdb") - 1) & " - " & Format(VBA.Date(), "dd-mmm-yyyy") & ".xls"

If Len(Dir(strWorkbookName)) > 0 Then
strMsg = strWorkbookName & " already exists." & vbCrLf & "Delete
it?"

Select Case MsgBox(strMsg, vbYesNoCancel + vbQuestion,
"Overwrite Verification")
Case vbYes
Kill strWorkbookName
Case vbCancel
Exit Sub
Case Else
End Select
End If

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err.Number = 0 Then
blnXLCreated = False
Else
Set oXL = CreateObject("Excel.Application")
blnXLCreated = True
End If

'On Error GoTo Err_ExportToExcel

oXL.Application.Workbooks.Add
Set oActiveWorkbook = oXL.Application.ActiveWorkbook

strSelectSQL = GetSelectSQL
strQuery = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set rs = CurrentDb.OpenRecordset(strSQL)

If (rs.BOF = False And rs.EOF = False) Then
intCurrentSheet = intCurrentSheet + 1

If (oActiveWorkbook.Worksheets.Count < intCurrentSheet) Then
oActiveWorkbook.Worksheets.Add
After:=oActiveWorkbook.Worksheets(intCurrentSheet - 1)
End If

With oActiveWorkbook.Worksheets(intCurrentSheet)
.name = strQuery

intCurrentColumn = 1

For Each fld In rs.Fields
.Cells(1, intCurrentColumn) = fld.name
intCurrentColumn = intCurrentColumn + 1
Next fld

.Cells(2, 1).CopyFromRecordset rs
.Rows("1:1").Font.Bold = True
.Columns.Autofit
End With

End If

rs.Close

oActiveWorkbook.Close SaveChanges:=True, FileName:=strWorkbookName

Set rs = Nothing
Set db = Nothing
Set oActiveWorkbook = Nothing

If blnXLCreated Then
oXL.Application.Quit
End If

Set oXL = Nothing
********************CODE**

And like I said, when it gets to the rs.fields it says something about
object or with block not set. Just figured I'd pick your brain while
you were here ;).

-Jayyde

Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're
referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


That's basically what I'm doing. It's been renamed. The literal
code I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here
Doug,
I'm
using your Excel export stuff as a base (trying anyway). What in
that
code
would make it create the recordset, let it pass through the .BOF &
.EOF =
false statement then throw errors on the fields of "Object variable
or
With
block variable not set"?

Thanx guys! =)
-Jayyde


message
First, be aware that there's a difference between a subform control
and
a
form being used as a subform. You've got a subform control on the
main
form.
One of the properties of that subform control is the Source Object,
which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a
subform.
However, it may end up with a different name. If you create your
subform
by
adding a subform control from the toolbox onto the main form, the
name
of
the subform control will definitely not be the same as the form
being
used
as a subform.

What you need to use in your code is the name of the subform
control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Need the help of all you gurus out there again. Alright, what I
have
going
this time is: 1 form that allows the user to select product type
and
which
fields they want included for an export to excel. Great, I
haven't
actually
tested the export yet, but I'm not there yet either. On this form
I
have
a
subform which is obviously not actually linked to anything and is
there
to
be a preview pane of sorts for the upcoming export. It's this
subform
that
is currently being a pain in my posterier. I've tried a few
different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go.
I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get
errors,
If
anyone knows an easier way to do this feel super-free to let me
know
what
it
is =).

Thanx ahead of time!
-Jayyde
 
Nothing jumps out at me.

Does the sample database that came with the column work for you?

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Jayyde said:
I tried it that way initially and only took that out because I wondered if
ADODB. would work any better, then tried it without explicitly declaring it
either way. None of them worked =\.

Douglas J. Steele said:
I'm wondering whether it's because it's DAO code, and you haven't
explicitly declared the objects as DAO.

See whether

Dim rs As DAO.Recordset
Dim fld As DAO.Field

works any better.

If you reread the column (July, 2005, available at
http://www.accessmvp.com/DJSteele/SmartAccess.html), I think you'll find I
was explicit in the sample code.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Jayyde said:
Yeah, the SQL's fine. I stopped it in there just to make sure, but it's
also the same SQL for that preview pane (which is working now thank God).
It's erroring out on the:

.Cells(1, intCurrentColumn) = fld.name

line.

Thanx, Doug
-Jayyde

p.s. Leaving work for weekend now, so I'll keep checking back but won't
have the code to test in front of me =\

So when you write the content of strSQL to the immediate window, copy it
into a query and run it, it runs correctly?

Do you know exactly what line of the Excel code it's complaining about?

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


lol. As far as the subform goes put the me. in there and still get the
error:
"The record source 'SELECT blah blah...' specified on this form or
report does not exist"

The "export stuff" was a pdf that you had on smart access or somesuch.
The code I'm trying is:


*******************CODE**
Dim oActiveWorkbook As Object
Dim oXL As Object
Dim oWorksheet As Object
Dim rs As Recordset
Dim fld As Field
Dim blnXLCreated As Boolean
Dim intCurrentColumn As Integer
Dim intCurrentSheet As Integer
Dim strMsg As String
Dim strSQL As String
Dim strWorkbookName As String
Dim strSelectSQL As String
Dim strQuery As String

strWorkbookName = CurrentDb().name
strWorkbookName = Left$(strWorkbookName, InStr(strWorkbookName,
".mdb") - 1) & " - " & Format(VBA.Date(), "dd-mmm-yyyy") & ".xls"

If Len(Dir(strWorkbookName)) > 0 Then
strMsg = strWorkbookName & " already exists." & vbCrLf & "Delete
it?"

Select Case MsgBox(strMsg, vbYesNoCancel + vbQuestion,
"Overwrite Verification")
Case vbYes
Kill strWorkbookName
Case vbCancel
Exit Sub
Case Else
End Select
End If

On Error Resume Next
Set oXL = GetObject(, "Excel.Application")

If Err.Number = 0 Then
blnXLCreated = False
Else
Set oXL = CreateObject("Excel.Application")
blnXLCreated = True
End If

'On Error GoTo Err_ExportToExcel

oXL.Application.Workbooks.Add
Set oActiveWorkbook = oXL.Application.ActiveWorkbook

strSelectSQL = GetSelectSQL
strQuery = GetQuery

strSQL = strSelectSQL & strQuerySQL

Set rs = CurrentDb.OpenRecordset(strSQL)

If (rs.BOF = False And rs.EOF = False) Then
intCurrentSheet = intCurrentSheet + 1

If (oActiveWorkbook.Worksheets.Count < intCurrentSheet) Then
oActiveWorkbook.Worksheets.Add
After:=oActiveWorkbook.Worksheets(intCurrentSheet - 1)
End If

With oActiveWorkbook.Worksheets(intCurrentSheet)
.name = strQuery

intCurrentColumn = 1

For Each fld In rs.Fields
.Cells(1, intCurrentColumn) = fld.name
intCurrentColumn = intCurrentColumn + 1
Next fld

.Cells(2, 1).CopyFromRecordset rs
.Rows("1:1").Font.Bold = True
.Columns.Autofit
End With

End If

rs.Close

oActiveWorkbook.Close SaveChanges:=True, FileName:=strWorkbookName

Set rs = Nothing
Set db = Nothing
Set oActiveWorkbook = Nothing

If blnXLCreated Then
oXL.Application.Quit
End If

Set oXL = Nothing
********************CODE**

And like I said, when it gets to the rs.fields it says something about
object or with block not set. Just figured I'd pick your brain while
you were here ;).

-Jayyde

Try putting in the reference to the main form (Me.), like I had

Afraid I don't know to what "Excel export stuff" code you're
referring.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


That's basically what I'm doing. It's been renamed. The literal
code I
have is:

subExportPreview.Form.RecordSource = strSQL

I'll add the me. and see what happens. But while I have you here
Doug,
I'm
using your Excel export stuff as a base (trying anyway). What in
that
code
would make it create the recordset, let it pass through the .BOF &
.EOF =
false statement then throw errors on the fields of "Object variable
or
With
block variable not set"?

Thanx guys! =)
-Jayyde


message
First, be aware that there's a difference between a subform control
and
a
form being used as a subform. You've got a subform control on the
main
form.
One of the properties of that subform control is the Source Object,
which
will be the actual form being used as a subform.

If you create your subform by dragging one form onto the other, the
subform
control is (usually) named the same as the form being used as a
subform.
However, it may end up with a different name. If you create your
subform
by
adding a subform control from the toolbox onto the main form, the
name
of
the subform control will definitely not be the same as the form
being
used
as a subform.

What you need to use in your code is the name of the subform
control.

Try Me.NameOfSubformControl.Form.Recordsource = strSQL


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Need the help of all you gurus out there again. Alright, what I
have
going
this time is: 1 form that allows the user to select product type
and
which
fields they want included for an export to excel. Great, I
haven't
actually
tested the export yet, but I'm not there yet either. On this form
I
have
a
subform which is obviously not actually linked to anything and is
there
to
be a preview pane of sorts for the upcoming export. It's this
subform
that
is currently being a pain in my posterier. I've tried a few
different
ways
so far. I've tried creating a queryDef then setting that as
[subform].sourceobject -- no go. I've also tried
[subform].form.recordsource = [theSQLstatement] -- also no go.
I've
checked
the SQL statement itself so I know that's good, incredibly long
potentially
but syntatically Ok. No matter which way I've tried this I get
errors,
If
anyone knows an easier way to do this feel super-free to let me
know
what
it
is =).

Thanx ahead of time!
-Jayyde
 
Back
Top